Entity Framework 4.1 Code First not creating tables

后端 未结 4 969
予麋鹿
予麋鹿 2020-12-08 23:37

I am using EF 4.1 and have created a repository using DBContext etc. Connection string set to point to a SQL Server 2008 R2 Dev edition.

When I run a console app th

4条回答
  •  隐瞒了意图╮
    2020-12-09 00:21

    To set the auto drop and create you would do something like this...

    public class MyDbContext : DbContext 
    {
        public IDbSet Foos { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer(new MyDbContextInitializer());
    
            base.OnModelCreating(modelBuilder);
        }
    }
    
    public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges
    {
        protected override void Seed(MyDbContext dbContext)
        {
            // seed data
    
            base.Seed(dbContext);
        }
    }
    

提交回复
热议问题