Separating ASP.NET Core 2.1 from class library of Entity Framework



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















When you read this question, you think it is a repeated question and that I did not do enough search for finding the solution of this issue. Unfortunately, I spent many hours searching for the solution for this issue. I am developing an ASP.NET Core 2.1 application, that consists mainly from ASP.NET Web APP and Entity Framework Core.



I am using the code first approach for adding migrations and updating database, and everything looks working fine, until I separated the asp.net core web app from the Entity Framework Core. Actually, I put the EntityFrameworkCore in separated class library.
When I tried to add new migration using



dotnet ef migrations add InitialCreate


I had this error




An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'ef, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
System.TypeLoadException: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c.b__12_5(TypeInfo t)
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator
1.MoveNext()
at System.Linq.Enumerable.ConcatIterator1.MoveNext()
at System.Linq.Enumerable.DistinctIterator
1.MoveNext()
at 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_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.




I am sure the problem is related to separation of the project, into component form



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


the above code is the code I am using for my ApplicationDBContext class that derived form IdentityContext, either add or remove MigrationAssembly this does not make a different. Also, adding IDbContextFactory does not make a big deal
I changed the ApplicationDbContext, that is derived from IdentityContext to the following



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











share|improve this question
























  • Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

    – Ivvan
    Nov 16 '18 at 12:41











  • I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

    – Omar Isaid
    Nov 16 '18 at 14:21

















0















When you read this question, you think it is a repeated question and that I did not do enough search for finding the solution of this issue. Unfortunately, I spent many hours searching for the solution for this issue. I am developing an ASP.NET Core 2.1 application, that consists mainly from ASP.NET Web APP and Entity Framework Core.



I am using the code first approach for adding migrations and updating database, and everything looks working fine, until I separated the asp.net core web app from the Entity Framework Core. Actually, I put the EntityFrameworkCore in separated class library.
When I tried to add new migration using



dotnet ef migrations add InitialCreate


I had this error




An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'ef, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
System.TypeLoadException: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c.b__12_5(TypeInfo t)
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator
1.MoveNext()
at System.Linq.Enumerable.ConcatIterator1.MoveNext()
at System.Linq.Enumerable.DistinctIterator
1.MoveNext()
at 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_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.




I am sure the problem is related to separation of the project, into component form



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


the above code is the code I am using for my ApplicationDBContext class that derived form IdentityContext, either add or remove MigrationAssembly this does not make a different. Also, adding IDbContextFactory does not make a big deal
I changed the ApplicationDbContext, that is derived from IdentityContext to the following



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











share|improve this question
























  • Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

    – Ivvan
    Nov 16 '18 at 12:41











  • I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

    – Omar Isaid
    Nov 16 '18 at 14:21













0












0








0








When you read this question, you think it is a repeated question and that I did not do enough search for finding the solution of this issue. Unfortunately, I spent many hours searching for the solution for this issue. I am developing an ASP.NET Core 2.1 application, that consists mainly from ASP.NET Web APP and Entity Framework Core.



I am using the code first approach for adding migrations and updating database, and everything looks working fine, until I separated the asp.net core web app from the Entity Framework Core. Actually, I put the EntityFrameworkCore in separated class library.
When I tried to add new migration using



dotnet ef migrations add InitialCreate


I had this error




An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'ef, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
System.TypeLoadException: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c.b__12_5(TypeInfo t)
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator
1.MoveNext()
at System.Linq.Enumerable.ConcatIterator1.MoveNext()
at System.Linq.Enumerable.DistinctIterator
1.MoveNext()
at 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_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.




I am sure the problem is related to separation of the project, into component form



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


the above code is the code I am using for my ApplicationDBContext class that derived form IdentityContext, either add or remove MigrationAssembly this does not make a different. Also, adding IDbContextFactory does not make a big deal
I changed the ApplicationDbContext, that is derived from IdentityContext to the following



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











share|improve this question
















When you read this question, you think it is a repeated question and that I did not do enough search for finding the solution of this issue. Unfortunately, I spent many hours searching for the solution for this issue. I am developing an ASP.NET Core 2.1 application, that consists mainly from ASP.NET Web APP and Entity Framework Core.



I am using the code first approach for adding migrations and updating database, and everything looks working fine, until I separated the asp.net core web app from the Entity Framework Core. Actually, I put the EntityFrameworkCore in separated class library.
When I tried to add new migration using



dotnet ef migrations add InitialCreate


I had this error




An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'ef, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
System.TypeLoadException: Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit)
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](MemberInfo element)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c.b__12_5(TypeInfo t)
at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator
1.MoveNext()
at System.Linq.Enumerable.ConcatIterator1.MoveNext()
at System.Linq.Enumerable.DistinctIterator
1.MoveNext()
at 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_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Could not load type 'EF.EFCore.ApplicationDbContext' from assembly 'EF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.




I am sure the problem is related to separation of the project, into component form



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


the above code is the code I am using for my ApplicationDBContext class that derived form IdentityContext, either add or remove MigrationAssembly this does not make a different. Also, adding IDbContextFactory does not make a big deal
I changed the ApplicationDbContext, that is derived from IdentityContext to the following



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








c# asp.net-core entity-framework-core asp.net-core-2.1 entity-framework-core-migrations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 at 10:14









ekad

12.3k123742




12.3k123742










asked Nov 15 '18 at 15:09









Omar IsaidOmar Isaid

138210




138210












  • Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

    – Ivvan
    Nov 16 '18 at 12:41











  • I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

    – Omar Isaid
    Nov 16 '18 at 14:21

















  • Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

    – Ivvan
    Nov 16 '18 at 12:41











  • I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

    – Omar Isaid
    Nov 16 '18 at 14:21
















Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

– Ivvan
Nov 16 '18 at 12:41





Under which path you execute migration command? You must run it in the project where your context is located. Also, you need to implement IDesignTimeDbContextFactory in your EF proejct.

– Ivvan
Nov 16 '18 at 12:41













I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

– Omar Isaid
Nov 16 '18 at 14:21





I created new WebApp of Asp.NetCore 2.1 , and creates new Entity core projects , and execute this console command dotnet ef migrations add NewMigration --project C:MyProjectsIEFCoreTest , where console command I execute it after creating the startup project and run the command inside it , when my vs studio finishes updating i will post my issue seeking for , explanation , and thank you

– Omar Isaid
Nov 16 '18 at 14:21












1 Answer
1






active

oldest

votes


















0














Our approach was to create a small helper-classlibrary with the following content:



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





And then we create the migration from inside the HelperForMigrations-Library-Folder with the following command:



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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322405%2fseparating-asp-net-core-2-1-from-class-library-of-entity-framework%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Our approach was to create a small helper-classlibrary with the following content:



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





    And then we create the migration from inside the HelperForMigrations-Library-Folder with the following command:



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






    share|improve this answer



























      0














      Our approach was to create a small helper-classlibrary with the following content:



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





      And then we create the migration from inside the HelperForMigrations-Library-Folder with the following command:



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






      share|improve this answer

























        0












        0








        0







        Our approach was to create a small helper-classlibrary with the following content:



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





        And then we create the migration from inside the HelperForMigrations-Library-Folder with the following command:



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






        share|improve this answer













        Our approach was to create a small helper-classlibrary with the following content:



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





        And then we create the migration from inside the HelperForMigrations-Library-Folder with the following command:



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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 13 at 10:32









        CompufreakCompufreak

        2,12711131




        2,12711131





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322405%2fseparating-asp-net-core-2-1-from-class-library-of-entity-framework%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20