分离ASP.NET核心2.1



当你读到这个问题时,你认为这是一个重复的问题,我没有做足够的搜索来找到这个问题的解决方案。不幸的是,我花了很多时间来寻找这个问题的解决方案。我正在开发一个ASP。NET核心2.1应用程序,它主要由ASP。NET Web应用程序和实体框架核心。

我使用代码优先的方法添加迁移和更新数据库,一切看起来都很好,直到我将asp.net核心web应用程序与实体框架核心分离。实际上,我把EntityFrameworkCore放在分离的类库中。当我尝试使用添加新的迁移时

dotnet ef migrations add InitialCreate

我有这个错误

访问类"Program"上的IWebHost时出错。在没有应用程序服务提供商的情况下继续。错误:无法加载类型"EF"。EFCore。ApplicationDbContext"来自程序集"ef,Version=2.1.4.0,Culture=neutral,PublicKeyToken=adb9793829ddae60"。系统TypeLoadException:无法加载类型"EF"。EFCore。ApplicationDbContext"来自程序集"EF,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"。在系统中。反射CustomAttribute_CreateCaObject(运行时模块pModule,IRuntimeMethodInfo pCtor,字节**ppBlob,字节*pEndBlob,Int32*pcNamedArgs(在系统中。反射CustomAttribute。CreateCaObject(RuntimeModule模块、IRuntimeMethodInfo ctor、IntPtr&blob、IntPtr-blobEnd、Int32&namedArgs(在系统中。反射CustomAttribute。GetCustomAttributes(RuntimeModule decoratedModule、Int32 decoratedMetadataToken、Int32 pcaCount、RuntimeType attributeFilterType、Boolean mustBeInheritable、IList derivedAttributes(在系统中。反射CustomAttribute。GetCustomAttributes(RuntimeType类型、RuntimeType caType、Boolean inherit(在系统中。属性GetCustomAttributes(MemberInfo元素,Type类型,布尔继承(在系统中。属性GetCustomAttribute(MemberInfo元素、Type attributeType、Boolean inherit(在系统中。反射CustomAttributeExtensions。GetCustomAttribute[T](MemberInfo元素(在微软。EntityFrameworkCore。设计内部的DbContextOperations<>c.b_112_5(类型信息t(在系统中。林克。可枚举。WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext((在系统中。林克。可枚举。凹面调制器1.MoveNext() at System.Linq.Enumerable.DistinctIterator1.MoveNext((在系统中。林克。可枚举。其中枚举迭代器1.MoveNext() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String name) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_01.b_0((在微软。EntityFrameworkCore。设计操作执行器。OperationBase。执行(动作(无法加载类型"EF"。EFCore。ApplicationDbContext"来自程序集"EF,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"。

我确信问题与项目分离有关,以组件形式

services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(conString,builder => builder.MigrationsAssembly(typeof(Startup).Assembly.FullName)));

上面的代码是我为ApplicationDBContext类使用的代码,该类派生自IdentityContext,添加或删除MigrationAssembly,这不会造成不同。此外,添加IDbContentFactory也没什么大不了的我将ApplicationDbContext(从IdentityContext派生(更改为以下

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Entity
string conString = "Server=(local); Database=OnlineStoreDB;User Id=sa;Password=123@asu;";
optionsBuilder.UseSqlServer(conString, x => x.MigrationsAssembly("EF"));
base.OnConfiguring(optionsBuilder);
// base.OnConfiguring(optionsBuilder);
//if (!optionsBuilder.IsConfigured)
//{
//    optionsBuilder.UseSqlServer(conString);
//}
//else
//{
//    optionsBuilder.UseSqlServer(conString);
//}
}

我们的方法是创建一个包含以下内容的小型助手类库:

public class ApiDbContextMigrationsFactory : IDesignTimeDbContextFactory<ApiDbContext>
{
public ApiDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApiDbContext>();
optionsBuilder.UseSqlServer(@"Server=(localdb)mssqllocaldb;Database=Api.Local.Migration;Trusted_Connection=True;ConnectRetryCount=0",
b => b.MigrationsAssembly(typeof(ApiDbContext).Assembly.FullName));
return new ApiDbContext(optionsBuilder.Options);
}
}

然后,我们使用以下命令从HelperForMigrationsLibrary文件夹中创建迁移:

dotnet ef migrations add nameofMigration --startup-project ./ --project ../ApiDbContextProject --context ApiDbContext

最新更新