2010-11-06 48 views
0

我试图遵循一般共识,最好把一个域对象放在一个单独的项目比他们的MVC项目,但得到以下编译器错误:编译错误使用EF4作为一个单独的项目比我的MVC项目

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

Source File: C:\Users\Kevin\documents\visual studio 2010\Projects\HandiGamer\HandiGamer.Domain\Entities\HGDomainModel.Designer.cs Line: 44

Line 42:   /// Initializes a new HGEntities object using the connection string found in the 'HGEntities' section of the application configuration file. 
Line 43:   /// </summary> 
Line 44:   public HGEntities() : base("name=HGEntities", "HGEntities") 
Line 45:   { 
Line 46:    this.ContextOptions.LazyLoadingEnabled = true; 

我有点困惑,因为HGEntities 有没有实体的app.config文件:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <connectionStrings> 
    <add name="HGEntities" connectionString="metadata=res://*/Entities.HGDomainModel.csdl|res://*/Entities.HGDomainModel.ssdl|res://*/Entities.HGDomainModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\HandiGamer.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 

我是否需要添加连接到我的MVC Web.Config什么的?

回答

2

是的,你需要将它添加到你的MVC Web.Config文件。

由于您有一个ASP.Net MVC项目,所以连接字符串必须存在于您的MVC项目的web.config中,运行时将寻找它。基本上,任何连接字符串都应该位于可执行项目的配置文件中(即通过CLR从.Net线程开始的地方)。所以只需将整个连接字符串复制并粘贴到您的web.conig中即可完成!

+0

我不知道该怎么做...有点新手当谈到这一些。介意让我朝正确的语法方向发展? – 2010-11-06 17:40:41

+0

当然,只需从app.config中复制整个连接字符串,然后转到您的MVC项目,打开它的web.config文件并将其粘贴到那里。 – 2010-11-06 17:44:09