purpose of Database.SetInitializer(null) inside of repository?

前端 未结 2 1699
刺人心
刺人心 2021-01-12 21:27

I am using entity framework and in my context inheriting from DbContext.

public class MyContext : DbContext, IMyContext
{
    static MyContext()
    {
               


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 21:37

    The default database initializer in Entity Framework Code First is CreateDatabaseIfNotExists. As its name indicates, if the database does not exist it'll create it.

    This behavior is good during development but when you go to production maybe you won't want to auto create your database.

    If you want to disable the initializers you use the line you showed, so now you have full control over how the database will be created and evolve in time.

    Other initializers:

    DropCreateDatabaseIfModelChanges.
    
    DropCreateDatabaseAlways
    
    Custom DB Initializer
    

    Check this to know more.

提交回复
热议问题