EF 4.1 Code First and Existing Database and .NET Membership

后端 未结 6 1472
南旧
南旧 2021-02-01 22:59

I have a database called ApplicationName_Development running on SQL Server 2008 R2 Developer edition on my development box.

I added .NET membership tables to the databas

6条回答
  •  死守一世寂寞
    2021-02-01 23:40

    This is how code-first work. Main idea of code first is that you do not touch your database because it is responsibility of the model to create the database. If you want to customize your database you must create custom IDatabaseInitializer and add your custom SQL.

    public class MyDbInitializer : DropCreateDatabaseIfModelChanges
    {
        protected override void Seed(MyContext context)
        {
            // Here run your custom SQL commands
            context.Database.ExecuteSqlCommand("CREATE TABLE ....");
        }
    }
    

    Now you only need setup your cutom intializer on the startup of your application:

    Database.SetInitializer(new MyDbInitializer());
    

    If you don't want to do it this way you must manually maintain your database and set initializer to null.

提交回复
热议问题