none
why can my entity framework not update database tables ? RRS feed

  • Question

  • i created a project with the simple sember ship, and add my models, but only tables of the simple sember ship were created.

    why ? why ? why ? no CarInfomation table .

    public class PinChedContext : DbContext
        {
            public DbSet<UserProfile> UserProfiles { get; set; }
            public DbSet<CarInfomation> CarInfomation { get; set; }
            

            public PinChedContext()
                : base("DefaultConnection")
            {
                Database.SetInitializer<PinChedContext>(new DropCreateDatabaseIfModelChanges<PinChedContext>()); 
            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            { 
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
            }
        }



    • Edited by sosolin Friday, June 17, 2016 3:31 AM
    Friday, June 17, 2016 3:29 AM

Answers

  • Hi sosolin,

    According to your code snippet, it seems that you want to use Database Initializer to update your database. Since you write Database Initializer code on you custom DbContext's construction method, so you need to create a PinChedContext instance to run the method. I create a console application by using your code, it works fine.

    namespace EFMigrationsWithDatabaseInitializer
    {
        using System.Data.Entity;
        using System.Data.Entity.ModelConfiguration.Conventions;
        public partial class EFDemoContext : DbContext
        {
            public EFDemoContext()
                : base("name=EFDemoContext")
            {
                Database.SetInitializer<EFDemoContext>(new DropCreateDatabaseIfModelChanges<EFDemoContext>());
            }
    
            public DbSet<UserProfile> UserProfiles { get; set; }
            public DbSet<CarInfomation> CarInfomation { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            }
        }
    }

    #Usage:

    using System;
    using System.Linq;
    
    namespace EFMigrationsWithDatabaseInitializer
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var db = new EFDemoContext())
                {
                    var result = db.CarInfomation.ToList();
    
                    foreach (var item in result)
                    {
                        Console.WriteLine(item.CarInfomationName);
                    }
    
                    Console.ReadKey();
                }
            }
        }
    }

    In addition, If the issue still is existed. Please change the model, and create a new PinChedContext instance and check if it updates you database.

    Best regards,

    Herro wong


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, June 20, 2016 1:32 AM
    Moderator