【问题标题】:Separating ASP.NET Core 2.1 from class library of Entity Framework将 ASP.NET Core 2.1 从实体框架的类库中分离出来
【发布时间】:2018-11-15 15:09:22
【问题描述】:

当您阅读此问题时,您认为这是一个重复的问题,并且我没有做足够的搜索来找到此问题的解决方案。不幸的是,我花了很多时间寻找这个问题的解决方案。我正在开发一个 ASP.NET Core 2.1 应用程序,主要由 ASP.NET Web APP 和 Entity Framework Core 组成。

我使用代码优先的方法来添加迁移和更新数据库,一切看起来都很好,直到我将 asp.net 核心 Web 应用程序与 Entity Framework Core 分开。实际上,我将 EntityFrameworkCore 放在单独的类库中。 当我尝试使用

添加新迁移时
dotnet ef migrations add InitialCreate

我遇到了这个错误

访问“程序”类上的 IWebHost 时出错。在没有应用程序服务提供商的情况下继续。错误:无法从程序集“ef,版本=2.1.4.0,文化=中性,PublicKeyToken=adb9793829ddae60”加载类型“EF.EFCore.ApplicationDbContext”。 System.TypeLoadException:无法从程序集“EF,版本=1.0.0.0,文化=中性,PublicKeyToken=null”加载类型“EF.EFCore.ApplicationDbContext”。 在 System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule,IRuntimeMethodInfo pCtor,字节** ppBlob,字节* pEndBlob,Int32* pcNamedArgs) 在 System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule 模块,IRuntimeMethodInfo ctor,IntPtr& blob,IntPtr blobEnd,Int32& namedArgs) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule 装饰模块,Int32 装饰元数据令牌,Int32 pcaCount,RuntimeType attributeFilterType,布尔 mustBeInheritable,IList 派生属性) 在 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType 类型,RuntimeType caType,布尔继承) 在 System.Attribute.GetCustomAttributes(MemberInfo 元素,Type 类型,布尔继承) 在 System.Attribute.GetCustomAttribute(MemberInfo 元素,类型 attributeType,布尔继承) 在 System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo 元素) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c.b__12_5(TypeInfo t) 在 System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.WhereEnumerableIterator1.MoveNext() 在 System.Linq.Enumerable.ConcatIterator1.MoveNext() at System.Linq.Enumerable.DistinctIterator1.MoveNext() 在 System.Linq.Enumerable.WhereEnumerableIterator1.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() 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作动作) 无法从程序集“EF,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”加载类型“EF.EFCore.ApplicationDbContext”。

我确定问题与将项目分离为组件形式有关

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

上面的代码是我用于派生自 IdentityContext 的 ApplicationDBContext 类的代码,添加或删除 MigrationAssembly 这并没有什么不同。此外,添加 IDbContextFactory 并没有什么大不了的 我将派生自 IdentityContext 的 ApplicationDbContext 更改为以下

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);
    //}

}

【问题讨论】:

  • 在哪个路径下执行迁移命令?您必须在您的上下文所在的项目中运行它。此外,您需要在 EF 项目中实现 IDesignTimeDbContextFactory。
  • 我创建了 Asp.NetCore 2.1 的新 WebApp,并创建了新的实体核心项目,并执行此控制台命令 dotnet ef migrations add NewMigration --project C:\MyProjectsI\EFCoreTest ,我执行控制台命令在创建启动项目并在其中运行命令后,当我的 vs studio 完成更新时,我将发布我的问题寻求,解释,谢谢

标签: c# asp.net-core entity-framework-core asp.net-core-2.1 entity-framework-core-migrations


【解决方案1】:

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

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);

    }
}

然后我们使用以下命令从 HelperForMigrations-Library-Folder 内部创建迁移:

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

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2010-10-29
    • 2020-06-02
    • 2016-12-19
    相关资源
    最近更新 更多