我想从ASP.NET MVC项目中首先使用SQL Server CE 4的EF代码,但不是在App_Data
文件夹中创建数据库,而是在我的工作站上安装的SQL Server Express实例中获得新数据库。安装SQL Server Express时,如何强制实体框架代码优先使用SQL Server CE?
如何获得在App_Data
文件夹中创建的数据库?
web.config
:
<connectionStrings>
<add name="NerdDinners" connectionString="Data Source=|DataDirectory|NerdDinners.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
Context
类:
public class NerdDinners : DbContext
{
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
编辑1
我想去阻止SQL Server Express的思维,可能迫使使用SQL Server CE的,但它导致了ProviderIncompatibleException
,内部例外为
{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"}
所以看来我的设置想要使用SQL Server Express。
编辑2 有人建议我在连接字符串名称,明确传递到的DbContext,所以我改变了我的上下文类以下几点:
public class NerdDinners : DbContext
{
public NerdDinners() : base("NerdDinners") { }
public DbSet<Dinner> Dinners { get; set; }
public DbSet<RSVP> RSVPs { get; set; }
}
在DinnersController控制器,这是上下文如何实例化:
public class DinnersController : Controller
{
private NerdDinners db = new NerdDinners();
//
// GET: /Dinners/
public ViewResult Index()
{
return View(db.Dinners.ToList());
}
做出这些更改后,数据库仍在SQL Server Express中创建,因此看起来我的连接字符串被忽略。
你有它配置如上,并没有得到sdf文件出现在斌/调试? – 2012-03-01 20:47:09
@Adam Tuliper - 这是正确的。我在本地SQL Server Express实例中获得一个新的数据库。 – DaveB 2012-03-01 20:51:34
没有其他连接字符串?还是安装了sqlce v 4? – 2012-03-01 20:53:27