我如何告诉EF如何命名数据库以及将其放在哪里?
如果Web.Config中没有连接字符串,它会尝试将其放入本地SQLEXPRESS Server中,但我想将其放到一个已知的SQL Server上,并按我想要的方式命名它。有什么建议吗?
发布于 2011-03-18 08:59:29
在app.config/web.config中创建一个与上下文同名的连接字符串,EF将使用该DB。
发布于 2012-03-08 04:48:44
如何在EF中使用不同的连接字符串名称
EF将在连接字符串中使用数据库的名称。当您想要将连接字符串的名称与EF分离时,您需要向构造函数提供您的连接字符串。示例:
public class DatabaseContext : DbContext
{
public DatabaseContext()
: base(ApplicationParameters.ConnectionStringName)
{
}
public DatabaseContext(string connectionStringName)
: base(connectionStringName)
{
}
}发布于 2014-05-13 03:20:44
在课堂上:
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。
Ref => http://brandonclapp.com/connection-strings-with-entity-framework-5-code-first/
https://stackoverflow.com/questions/5346926
复制相似问题