SQL Server 2008中的实体框架4.1 code连接字符串第一 [英] SQL Server 2008 connection string for entity framework 4.1 code first

查看:85
本文介绍了SQL Server 2008中的实体框架4.1 code连接字符串第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为实体框架4.1 code-第一个项目一个有效的SQL Server 2008的连接字符串。我将与MVC 3,现在使用它。

I need a valid SQL Server 2008 connection string for Entity Framework 4.1 code-first project. I would use it now with MVC 3.

目前,它仍然很简单,只有1个项目,3个简单的模型类...

For now it's still very simple, only 1 project, 3 simple model class...

我只能找到一切,如SQL防爆preSS,在网络上CE连接...

I could only find everything else, like Sql Express, CE connections on the web...

查找它的名字在的web.config (ApplicationServices)就OK了,因为当我试图用我特定的错误行为。

Finding it by name in web.config ("ApplicationServices") is OK, because when I tried to use I got specific errors for that.

我能得到的最好的是:

无法加载指定的元数据
  资源

Unable to load the specified metadata resource.

我试着给它像 =元数据资源://MyWebProject/MyWebProject.csdl | ... ,但也没有成功。

I tried to give it like metadata=res://MyWebProject/MyWebProject.csdl| ... also but no success.

所以它不会为我创建数据库 - 即便不打 OnModelCreating(DbModelBuilder模型构建器)

So it doesn't create the database for me - even doesn't hit the OnModelCreating(DbModelBuilder modelBuilder) method.

如果我尝试使用像我找到了SQL Server的防爆preSS的那些一个老土的连接,它忽略了元数据。

If I try to use an 'old fashioned' connection like the ones I found for SQL Server Express, it misses the metadata.

感谢您的帮助提前。

推荐答案

的code-第一的想法是,你不应该有对付提到的.csdl,.ssdl和.msl文件在连接串。如果没有其他指定,将的DbContext如你所描述查找在web.config中的连接字符串。中的DbContext类的构造函数将接受一个名称 - 值对specifiying在web.config中的连接字符串的名称。例如:

The idea of "Code-First" is, that you shouldn't have to deal with the .csdl, .ssdl and .msl files mentioned in the connection-string. If not specified elsewhere, the DbContext will lookup the connection-string in the web.config as you described. The constructor of the DbContext class will accept a name-value pair specifiying the name of the connection-string in the web.config. For example:

<connectionStrings>
    <add name="ConnectionName" 
         providerName="System.Data.SqlClient"
         connectionString="Server=ServerName;Database=DatabaseName;Integrated Security=True;" />
</connectionStrings>

可以在上下文中引用的:

can be referenced in your context:

class MyContext : DbContext
{
     public MyContext() : base("name=ConnectionName") { }
     ...
}

我提供的样品连接字符串实际上是一个SQL Server数据库。指定的ProviderName,自code-第一需要它来生成相应的.ssdl-文件(存储模式)。

The sample connection-string I've provided is actually for a SQL Server database. It is important to specify the ProviderName, since the "Code-First" requires it to generate a corresponding .ssdl-File (storage schema).

这篇关于SQL Server 2008中的实体框架4.1 code连接字符串第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆