asp.net-mvc-3 - Entity Framework 4.1 代码优先的 SQL Server 2008 连接字符串

标签 asp.net-mvc-3 configuration entity-framework-4 web-config entity-framework-4.1

我需要一个用于 Entity Framework 4.1 代码优先项目的有效 SQL Server 2008 连接字符串。我现在将它与 MVC 3 一起使用。

目前还是很简单,只有1个项目,3个简单的模型类...

我只能在网络上找到其他所有内容,例如 Sql Express、CE 连接...

web.config(“ApplicationServices”)中按名称查找它是可以的,因为当我尝试使用时,我遇到了特定的错误。

我能得到的最好的是:

Unable to load the specified metadata resource.

我试图给它像 metadata=res://MyWebProject/MyWebProject.csdl| ... 也但没有成功。

因此它不会为我创建数据库 - 甚至不会调用 OnModelCreating(DbModelBuilder modelBuilder) 方法。

如果我尝试使用“老式”连接(例如我为 SQL Server Express 找到的连接),它会丢失元数据。

感谢您提前提供的帮助。

最佳答案

“代码优先”的想法是,您不必处理连接字符串中提到的 .csdl、.ssdl 和 .msl 文件。如果没有在其他地方指定,DbContext 将按照您的描述在 web.config 中查找连接字符串。 DbContext 类的构造函数将接受指定 web.config 中连接字符串名称的名称-值对。例如:

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

可以在您的上下文中引用:

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

我提供的示例连接字符串实际上适用于 SQL Server 数据库。指定 ProviderName 非常重要,因为“代码优先”要求它生成相应的 .ssdl 文件(存储架构)。

关于asp.net-mvc-3 - Entity Framework 4.1 代码优先的 SQL Server 2008 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6575976/

相关文章:

json - MVC3 自动绑定(bind)到 JSON 对象列表不适用于属性

c# - Fluent NHibernate - 配置持久层时发生异常

asp.net - Entity Framework 4 Linq non Lazy Loaded .Include(),包含当前实体的子实体的相关实体

entity-framework-4 - Entity Framework 中的多个SaveChanges调用

c# - 组合框和 Entity Framework

c# - 如何在不使用youtube API的情况下解析youtube xml

c# - ASP.NET MVC3 WebGrid 格式 : parameter

c# - 在 MVC 中获取禁用的下拉值

java - Spring MVC Controller 配置 - 混合注释和 XML @PathVariable

spring - Spring中的DispatcherServlet和ContextLoaderListener