locked
How to auto create suppliers table, and migrate changes to db. RRS feed

  • Question

  • User-570626059 posted

    I have created code first entity model based on a table in my localdb.

    My question is how do I now get this to migrate and update the localdb table with new fields. When i do add-migration it comes up blank with no changes.

    Also my next question is currently I am using test sql db in development. When this launches to a new sql server, I want it to create a database and the associated models as tables?

    How do i do this?

    Thursday, July 5, 2018 11:12 AM

All replies

  • User1520731567 posted

    Hi skyblue28,

    My question is how do I now get this to migrate and update the localdb table with new fields. When i do add-migration it comes up blank with no changes.

    Also my next question is currently I am using test sql db in development. When this launches to a new sql server, I want it to create a database and the associated models as tables?

    If it comes up blank with no changes, it seems that there's no change for db or model.

    I suggest you could follow the steps to implement code first migration.

    1.Add the latest version of the EntityFramework NuGet package to the project

          Tools –> Library Package Manager –> Package Manager Console
          Run the Install-Package EntityFramework command

    2.Create the Model

    3.Create a Context

    4.Statement this model in the Context,like:

     using System.Data.Entity;
        using System.Collections.Generic;
        using System.ComponentModel.DataAnnotations;
        using System.Data.Entity.Infrastructure;
    
        namespace MigrationsDemo
        {
            public class BlogContext : DbContext
            {
                public DbSet<Blog> Blogs { get; set; }
            }
    
            public class Blog
            {
                public int BlogId { get; set; }
                public string Name { get; set; }
            }
        }

    5.Run the Enable-Migrations command in Package Manager Console

    6.Run the Add-Migration XXXX command in Package Manager Console,It will generate a migration file which filename with timestamp.

    7.Run the Update-Database command in Package Manager Console,it will generate a database.

    If you have any changes, you need to run Add-Migration XXXX and Update-Database in the Console.

    More details, you could refer to these links:

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

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

    Best Regards.

    Yuki Tao

    Friday, July 6, 2018 5:44 AM
  • User-570626059 posted


    Enable-Migrations -MigrationsDirectory "Migrations\ProductsContext" -ContextTypeName PrincePortalWeb.Products
    Add-Migration initialmigration -ConfigurationTypeName SupplierContextConfig
    update-datebase -verbose -ConfigurationTypeName SupplierContextConfig

    the above seem to fix it for me. I had to specify a folder for each context representing a table in my db. Then rename the configuration file and reference this as above.

    Friday, July 6, 2018 8:48 AM