1.概要
1.1 情景
?使用ef生成数据库
1.2.生成数据库使用的命令
1.2.1生成一个配置类命令:Enable-Migrations -EnableAutomaticMigrations
1.2.2生成一个数据库更新脚本文件:Add-Migration InitialCreate
1.2.3生成数据库文件的命令:Update-Database -Verbose
2.代码
2.1 依赖添加
2.1?
using MySql.Data.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF生成数据库命令
{
class Program
{
static void Main(string[] args)
{
}
[Table("Tb1")]
public class Tb1
{
public int id { set; get; }
public int v1 { set; get; }
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class DBContentMy : DbContext
{
public DBContentMy() : base("DbConnStr")
{
Database.SetInitializer<DBContentMy>(null);
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Tb1> Tb1s { set; get; }
}
}
}
1.2
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DbConnStr" connectionString="Data Source=localhost;port=3306;Initial Catalog=db2;user id=root;password=123456;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.28.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider></providers>
</entityFramework>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
2.2?Enable-Migrations -EnableAutomaticMigrations
namespace EF生成数据库命令.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<EF生成数据库命令.Program.DBContentMy>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(EF生成数据库命令.Program.DBContentMy context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data.
}
}
}
2.3?Add-Migration InitialCreate
namespace EF生成数据库命令.Migrations { ? ? using System; ? ? using System.Data.Entity.Migrations; ? ?? ? ? public partial class InitialCreate : DbMigration ? ? { ? ? ? ? public override void Up() ? ? ? ? { ? ? ? ? ? ? CreateTable( ? ? ? ? ? ? ? ? "dbo.Tb1", ? ? ? ? ? ? ? ? c => new ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? id = c.Int(nullable: false, identity: true), ? ? ? ? ? ? ? ? ? ? ? ? v1 = c.Int(nullable: false), ? ? ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? ? .PrimaryKey(t => t.id); ? ? ? ? ? ?? ? ? ? ? } ? ? ? ?? ? ? ? ? public override void Down() ? ? ? ? { ? ? ? ? ? ? DropTable("dbo.Tb1"); ? ? ? ? } ? ? } }?
2.4
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.2.0" targetFramework="net452" />
<package id="EntityFramework.zh-Hans" version="6.2.0" targetFramework="net452" />
<package id="Microsoft.AspNet.Cors" version="5.0.0" targetFramework="net46" />
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Core.zh-Hans" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.EntityFramework" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.EntityFramework.zh-Hans" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.AspNet.Identity.Owin.zh-Hans" version="2.2.1" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Cors" version="3.0.1" targetFramework="net46" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
<package id="MySql.Data" version="6.10.9" targetFramework="net452" />
<package id="MySql.Data.Entity" version="6.10.9" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net46" />
</packages
3.运行结果
CREATE TABLE `tb1` (
`id` int NOT NULL AUTO_INCREMENT,
`v1` int NOT NULL,
PRIMARY KEY (`id`)
)
4.引用
?ef数据迁移命令总结之Update-Database_一头小驴的博客-CSDN博客_ef update-database
EF Add-Migration总结 - 雨中超越 - 博客园?
ef数据迁移命令总结之 Enable-Migrations_一头小驴的博客-CSDN博客_enable-migration
|