0

I am building a application with ASP.NET Core MVC6 and Entity Framework Core code first with build in DB context, the SQL database has already been populated with data records. I recently made some small changes to the data models and recreate the migration, with commands as ("dotnet ef migrations add Stage3", "dotnet ef database update") in VS 2015 Package Manager Console, but it ran into error as:

dotnet.exe : System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Company' in the database.

Company table is on the top of the tables relationship, it seems that because the table Company is already there and the EF can not update the new table structure. If I change DB name in the connection string, it will create new database with new table structure without any issues. I am not sure how to address this issue? After the application go live in the near future I will properly make more changes to the Modes and will have same issue again and I cannot delete database with live data to recreate new table structure, Maybe I should configure it in the Startup.cs file, but I haven't found any useful resources yet. Please give me some advises.

I have attempted to change the DB Initializer as attached screenshot, but not sure how to do it. enter image description here

I checked the project code again, the migration has not been applied to __MigrationHistory table, the migration code actually contained the code to create whole database structure as sample below:

 migrationBuilder.CreateTable(
            name: "Company",
            columns: table => new
            {
                CompanyId = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                CompanyName = table.Column<string>(maxLength: 100, nullable: false),
                IsAdmin = table.Column<bool>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Company", x => x.CompanyId);
            });

And I haven't changed the project namespace. Recently I just made some changes on few table relationships such as site user permission table(company has many sites). I added a permission table, so now site user permission table can have multiple permissions type instead of single permission type.

Not sure how to set up automatic migrations in Entity framework core.

bearview
  • 51
  • 6
  • Can you post changes made to company? – Yaser Nov 29 '16 at 08:38
  • I haven't made changes to the company table, Recently I just made some changes on few table relationships such as site user permission table(company has many sites). I added a permission table, so now site user permission table can have multiple permissions type instead of single permission type. – bearview Nov 30 '16 at 22:07

1 Answers1

0

In Entity framework code first approch, there are four different database initialization strategies:

  • CreateDatabaseIfNotExists: This is default initializer. As the name suggests, it will create the database if none exists as per the configuration. However, if you change the model class and then run the application with this initializer, then it will throw an exception.

  • DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes (entity classes) have been changed. So you don't have to worry about maintaining your database schema, when your model classes change.

  • DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application, irrespective of whether your model classes have changed or not. This will be useful, when you want fresh database, every time you run the application, like while you are developing the application.

  • Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or you want to do some other process that initializes the database using the above initializer.

So if you are using DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways then replace it with CreateDatabaseIfNotExists.

Please try this out.

J-Mean
  • 1,192
  • 1
  • 8
  • 14
  • Hi, thanks for your reply, but I have already used the default initializer in the ApplicationDbContext.cs file, which I assume is CreateDatabaseIfNotExists, please advise. – bearview Nov 30 '16 at 03:56
  • Okay Please can you try this link http://stackoverflow.com/questions/24169140/there-is-already-an-object-named-aspnetroles-in-the-database – J-Mean Nov 30 '16 at 07:00
  • Thanks for your reply, this example is with old version of Entity framework, I am using EF core, so the -IgnoreChanges command parameter is not available in EF core, not sure how to set up automatic migrations in Entity framework core? – bearview Nov 30 '16 at 22:16
  • Sorry I didn't notice in question that you had faced this issue in EF core. Will this link be helpful for you set Auto migrations in EF core http://druss.co/2016/01/asp-net-core-1-0-automatic-migrations-entity-framework-7-ef-core-1-0 – J-Mean Dec 01 '16 at 08:29
  • Hi, I have tried to apply the context.Database.Migrate() to my seed data class, but the Migrate() method is not available. Also it seem only apply to running the MVC project. The issue I got is when I use the EF core migration command to update database in VS 2015 Package Manager Console, so not yet be able to run the project yet. – bearview Dec 02 '16 at 06:02