Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Turning Off Code First Migration #2994


User avatar
0
llarouche created

Hello;

I have a large database that I am integrating with the Asp.net Zero Starter App. I don't want to generated the database tables from the Code. I will in fact generated the code from the database as the 100s of tables are already exists. I did add the Audit Fields to match the the Abp approach.

How can I turn off Code First Migrations correctly?

This morning I started have exceptions thrown after I added a DbSet in the DataContext. The DbSet I add is for a table that has already been merged with the other AbpXxxxxx Tables.

An error occurred while starting the application.

InvalidOperationException: The model backing the 'DlwDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (<a class="postlink" href="http://go.microsoft.com/fwlink/?LinkId=238269">http://go.microsoft.com/fwlink/?LinkId=238269</a>). System.Data.Entity.CreateDatabaseIfNotExists.InitializeDatabase(TContext context)

TargetInvocationException: Exception has been thrown by the target of an invocation. System.RuntimeMethodHandle.InvokeMethod(object target, Object[] arguments, Signature sig, bool constructor)

ComponentActivatorException: ComponentActivator: could not instantiate Kw21.Dlw.EntityFramework.DlwDbContext Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructor, Object[] arguments, Type implType)

InvalidOperationException: The model backing the 'DlwDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (<a class="postlink" href="http://go.microsoft.com/fwlink/?LinkId=238269">http://go.microsoft.com/fwlink/?LinkId=238269</a>). System.Data.Entity.CreateDatabaseIfNotExists.InitializeDatabase(TContext context) System.Data.Entity.Internal.InternalContext+<>c__DisplayClassf.<CreateInitializationAction>b__e() System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) System.Data.Entity.Internal.RetryAction.PerformAction(TInput input) System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action<InternalContext> action) System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext() Abp.EntityFramework.AbpDbContext.RegisterToChanges() Abp.Zero.EntityFramework.AbpZeroDbContext..ctor(string nameOrConnectionString) Kw21.Dlw.EntityFramework.DlwDbContext..ctor(string nameOrConnectionString) in DlwDbContext.cs + : base(nameOrConnectionString)

Please let me know.

Regards,

Leonard (Dino) Larouche


5 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I have followed below approach in one of my previous projects.

    1. I have created POCO entities from existing database tables.
    2. Then I have created the initial migration file and deleted it's content.
    3. Applied this empty initial migration to database.
    4. Then I was able to use code first migrations for my new entities.

    If you completely want to disable migrations, you can change this line <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.EntityFramework/AbpZeroTemplateDataModule.cs#L17">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L17</a> to

    Database.SetInitializer(null);
    

    Thanks.

  • User Avatar
    0
    exlnt created

    <cite>ismcagdas: </cite> Hi,

    I have followed below approach in one of my previous projects.

    1. I have created POCO entities from existing database tables.
    2. Then I have created the initial migration file and deleted it's content.
    3. Applied this empty initial migration to database.
    4. Then I was able to use code first migrations for my new entities.

    If you completely want to disable migrations, you can change this line <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.EntityFramework/AbpZeroTemplateDataModule.cs#L17">https://github.com/aspnetzero/aspnet-ze ... ule.cs#L17</a> to

    Database.SetInitializer(null);
    

    Thanks.

    Can you please share the steps for doing this in .NET Core template?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @exlnt,

    I haven't done this with EF Core. Let me try a sample scenario and share my results with you.

    I will probaby share it in this week.

    Thanks.

  • User Avatar
    0
    ultimatemm created

    Hi ismcagdas, I also want to know step by step approach of migrations for existing DB table and sp,udf to aspzero starter app flow.:)

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ultimatemm,

    Actually AspNet Zero does not offer a solution for this officially. I just shared my own experience :).

    There might be many problems using AspNet Zero with an existing database. For example, if you have a user table in your existing database, it will conflict with AspNet Zero's Users table. This is valid for other existing tables of AspNet Zero.

    If you are using EF 6.x, you can try above steps I have shared before.

    Thanks.