1

我有两个项目

MyProject //MVC 3 app
MyProject.DAL //Class Library project type

里面MyProject.DAL有一个EntityModels包含生成实体的文件夹(EF Code-First 方法):

namespace MyProject.DAL.EntityModels
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class myEntities : DbContext
    {
        public myEntities() : base("name=myEntities")
        {
             ...
        }
    }
}

应用程序配置:

 <add name="myEntities" connectionString="metadata=res://*/EntityModels.DBMainModel.csdl|res://*/EntityModels.DBMainModel.ssdl|res://*/EntityModels.DBMainModel.msl;provider=..." providerName="System.Data.EntityClient" />

然后,我想在我的项目中使用该实体,所以我在文件MyProject中添加了相同的连接字符串。web.config

但是,我得到了Unable to load the specified metadata resource.错误。web.config我试着做一些修改

<add name="myEntities"
connectionString="metadata=res://*/MyProject.DAL.EntityModels.DBMainModel.csdl|
                           res://*/MyProject.DAL.EntityModels.DBMainModel.ssdl|
                           res://*/MyProject.DAL.EntityModels.DBMainModel.msl;provider=..." providerName="System.Data.EntityClient" />

<add name="myEntities"
connectionString="metadata=res://MyProject.DAL.EntityModels.DBMainModel.csdl|
                           res://MyProject.DAL.EntityModels.DBMainModel.ssdl|
                           res://MyProject.DAL.EntityModels.DBMainModel.msl;provider=..." providerName="System.Data.EntityClient" />


<add name="myEntities"
connectionString="metadata=res://MyProject.DAL/EntityModels.DBMainModel.csdl|
                           res://MyProject.DAL/EntityModels.DBMainModel.ssdl|
                           res://MyProject.DAL/EntityModels.DBMainModel.msl;provider=..." providerName="System.Data.EntityClient" />

但没有任何效果。如何解决?

4

1 回答 1

0

如果它是代码优先的,则应将纯 ​​connectionString 值放入适当的 .config 文件中(您正在处理SqlClientnot EntityClient)。
要了解有关 ConnectionString 值的更多信息,请查看http://www.connectionstrings.com/sql-server/

对于 SQL Server 数据库,基本上如下所示:

<add name="MyEntities" connectionString="Data Source=myServerName\myInstanceName;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" providerName="System.Data.SqlClient"/>
于 2013-10-31T12:14:39.937 回答