ef core 3 migration

先选中要migration的项目,个人理解,要migration的项目有以下特点:

1. 有appsetting.json文件,文件里当然配好了连接串,等会migration的时候就是使用这里面配的连接串的;

2. 在startup.cs文件里,可以某种方式进到如下类似的代码里:

services.AddDbContext<TIdentityDbContext>(options => options.UseMySql(identityConnectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));

其实就是注册 DbContext 的代码。什么叫 以某种方式进到 ?简单说就是直接写了或者调用了在别的项目里写的这么一行代码。

本人常见的的,就是MVC web类型的项目。

migration步骤,执行以下命令:

1.

dotnet ef migrations add InitialCreate --context AdminIdentityDbContext

2.

dotnet ef database update --context AdminIdentityDbContext

执行以上两个命令的时候所在的目录就算是target project,migration assembly和target project好像要一致,为什么是好像?因为不一致它就报错如下,而我也没找哪里特别说明这两货要一致的文本(没仔细找^_^)。

Your target project 'MyISAdmin.STS.Identity' doesn't match your migrations assembly 'MyISAdmin.Admin.EntityFramework.MySql'. Either change your target project or cChange your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("MyISAdmin.STS.Identity")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

可以看到最上面一处代码里有调用 sql.MigrationsAssembly(migrationsAssembly) 这么个代码,这里的 migrationsAssembly 就是个字符串,可以hard code,是否要hard code,自己斟酌。

猜你喜欢

转载自www.cnblogs.com/lihan829/p/12622990.html