locked
Entity framework Code first RRS feed

  • Question

  • User-203654341 posted

    Hi,

    I am using Entity framework code first approach on an existing database & it looks like a new table dbo._migrations is created. What is the purpose of the table & is it created in production too if i move the code base to production??

    Thanks....

    Tuesday, November 21, 2017 11:42 PM

Answers

  • User1400794712 posted

    Hi Chethan1221,

    1. dbo.__MigrationHistory is a table to store the details of each migration. And it's created when applying the first migration to database. When we want to modify the database according to entity class, we can use migration.
    2. When we use code first, this table will be created automatically. If you move code to production, it will also be created in production.
    3. Here is an article about code first migration with existing database, you can refer to it.

            https://msdn.microsoft.com/en-us/library/dn579398(v=vs.113).aspx

    Best Regards,
    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2017 5:11 AM
  • User991499041 posted

    Hi Chethan1221,

    Is the table created even if the DB is not sql server? What if i use empty Model does it still need the table( as there wouldn't be any changes to entity)??

    What if Database is already created & have data in it, does code first approach drop the entire database & create a new DB with dbo._MigrationHistory table??

    In addition to SQL Server, you can also use the Entity Framework Code-First in databases such as MySQL, Oracle, etc.

    When the application is running in production, it is usually storing data that you want to keep, and you don't want to lose everything each time you make a change such as adding a new column. The Code First Migrations feature solves this problem by enabling Code First to update the database schema instead of dropping and re-creating the database.

    When you run the deployed application and it accesses the database for the first time after deployment, Code First checks if the database matches the data model. If there's a mismatch, Code First automatically creates the database (if it doesn't exist yet) or updates the database schema to the latest version (if a database exists but doesn't match the model).

    Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application 

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 3:20 AM

All replies

  • User1400794712 posted

    Hi Chethan1221,

    1. dbo.__MigrationHistory is a table to store the details of each migration. And it's created when applying the first migration to database. When we want to modify the database according to entity class, we can use migration.
    2. When we use code first, this table will be created automatically. If you move code to production, it will also be created in production.
    3. Here is an article about code first migration with existing database, you can refer to it.

            https://msdn.microsoft.com/en-us/library/dn579398(v=vs.113).aspx

    Best Regards,
    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2017 5:11 AM
  • User-203654341 posted

    Thanks Daisy for the detailed explanation, appreciate it. 

    Is the table created even if the DB is not sql server? What if i use empty Model does it still need the table( as there wouldn't be any changes to entity)??

    What if Database is already created & have data in it, does code first approach drop the entire database & create a new DB with dbo._MigrationHistory table??

    Tuesday, November 28, 2017 8:38 PM
  • User991499041 posted

    Hi Chethan1221,

    Is the table created even if the DB is not sql server? What if i use empty Model does it still need the table( as there wouldn't be any changes to entity)??

    What if Database is already created & have data in it, does code first approach drop the entire database & create a new DB with dbo._MigrationHistory table??

    In addition to SQL Server, you can also use the Entity Framework Code-First in databases such as MySQL, Oracle, etc.

    When the application is running in production, it is usually storing data that you want to keep, and you don't want to lose everything each time you make a change such as adding a new column. The Code First Migrations feature solves this problem by enabling Code First to update the database schema instead of dropping and re-creating the database.

    When you run the deployed application and it accesses the database for the first time after deployment, Code First checks if the database matches the data model. If there's a mismatch, Code First automatically creates the database (if it doesn't exist yet) or updates the database schema to the latest version (if a database exists but doesn't match the model).

    Code First Migrations and Deployment with the Entity Framework in an ASP.NET MVC Application

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application 

    Regards,

    zxj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 3:20 AM