How to specify database name in Code First?

后端 未结 7 1343
广开言路
广开言路 2020-12-13 06:00

How do I tell EF what to name the database and where to put it?

If there is no connection string in the Web.Config, it tries to put it in the local SQLEXPRESS Server

7条回答
  •  清歌不尽
    2020-12-13 06:23

    How to Use a Different Connection String Name with EF

    EF will use the name of the database in the connection string. When you want to decouple the name of your connection string from EF, you need to provide your connection string to the constructor. Example:

    public class DatabaseContext : DbContext
    {
        public DatabaseContext() 
          : base(ApplicationParameters.ConnectionStringName)
        {
        }
    
        public DatabaseContext(string connectionStringName)
          : base(connectionStringName)
        {
        }
    
    }
    

提交回复
热议问题