【问题标题】:How to specify database name in Code First?如何在 Code First 中指定数据库名称?
【发布时间】:2011-07-17 20:15:30
【问题描述】:

我如何告诉 EF 为数据库命名什么以及放在哪里?

如果 Web.Config 中没有连接字符串,它会尝试将其放在本地 SQLEXPRESS Server 中,但我想将其放在已知的 SQL Server 上并命名为我想要的。有什么建议吗?

【问题讨论】:

  • 您可以通过像这样public MyContext() : base("NameForTheDB") { } 链接上下文的构造函数来命名数据库

标签: entity-framework-4 ef-code-first


【解决方案1】:

在 app.config/web.config 中创建一个与上下文同名的连接字符串,EF 将使用该数据库。

【讨论】:

【解决方案2】:

如前所述,您可以在应用程序的配置文件中使用名称(例如 “YourDBName”)声明连接字符串,然后将其传递给 DbContext 基本构造函数调用(我会将其添加到答案中以提供完整的答案 - 已经给出了很好的答案)。

或者,您可以使用Database.Connection.ConnectionString 属性在您的DbContext 扩展类中以编程方式设置它。例如:

App.config:

<!-- More.... -->
<!-- You can do this in a declarative way -->
<connectionStrings>
  <add name="YourDBName"
       connectionString="<Your connection string here>"
       providerName="<Your provider here>" />
</connectionStrings>
<!-- More.... -->

DatabaseContext.cs:

public class DatabaseContext : DbContext
    //Link it with your config file
    public DatabaseContext () : base("YourDBName") 
    {
        //And/Or you can do this programmatically.
        this.Database.Connection.ConnectionString = "<Your Connection String Here>";
        // More Stuff.....
    }
}

【讨论】:

  • 我也喜欢这个答案。作为 EF 的新手,这对我来说是最有意义的;使用适当的语言来描述正在发生的事情。对方的解释不清楚到底是什么命名数据库。
【解决方案3】:

作为参考,这里是如何使用 VB.NET 在代码中做到这一点:

Public Class DatabaseContext : Inherits DbContext

Public Property Users As DbSet(Of User)

Public Sub New()
    MyBase.New("NewFileName.sdf")
End Sub

结束类

【讨论】:

    【解决方案4】:

    在课堂上:

    public class Context : DbContext
    {
        //SET CONNECTION STRING NAME FOR DataBase Name :
        public Context() : base("YourConnectionName") { }
    
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }
    }
    

    在 web.config 中:

    <connectionStrings>  
        <add name="YourConnectionName" connectionString="Data Source=A-PC\SQLEXPRESS;
        Initial Catalog=MyDataBase; Integrated Security=True" 
        providerName="System.Data.SqlClient" />
    </connectionStrings>  
    

    感谢 ferventcoder。
    参考 => http://brandonclapp.com/connection-strings-with-entity-framework-5-code-first/

    【讨论】:

      【解决方案5】:

      如何在 EF 中使用不同的连接字符串名称

      EF 将在连接字符串中使用数据库的名称。当您想将连接字符串的名称与 EF 分离时,您需要将连接字符串提供给构造函数。示例:

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

      【讨论】:

        【解决方案6】:

        您也可以在 DbContext 构造函数中设置名称。

        【讨论】:

        【解决方案7】:

        如果您将连接字符串指向现有数据库,则 EF “代码优先”将不会尝试自动创建一个。

        EF“代码优先”使用一种约定,默认情况下上下文类会查找与上下文类同名的连接字符串。

        Using ef code first with an existing database

        【讨论】:

          猜你喜欢
          • 2012-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-22
          • 2014-11-03
          • 1970-01-01
          • 2013-12-09
          相关资源
          最近更新 更多