【发布时间】:2013-01-26 19:42:20
【问题描述】:
让我澄清一下我的情况。我有一个使用库的 WCF 服务,在这个库中存在数据库模型 (edmx)。
在我的 WCF 服务中:
[DataContract]
public class QuestionSetInformation
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string SetName { get; set; }
[DataMember]
public string ObjectiveName { get; set; }
}
[ServiceContract]
public interface IService1
{
[OperationContract]
QuestionSetInformation[] GetQuestionSets(string objectiveName);
}
public QuestionSetInformation[] GetQuestionSets(string objectiveName)
{
var query = from r in QuestionRepositoryManager.GetRepositories()
select new QuestionSetInformation()
{
Id = r.Id,
SetName = r.SetName,
ObjectiveName = r.ObjectiveName
};
return query.ToArray();
}
由于我的库使用 EDMX,所以我将连接字符串移到了我的 WCF,它使用 WCF 测试客户端完美运行。它满足了我的期望。
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ContosoDb" connectionString="metadata=res://*/Entities.Model1.csdl|res://*/Entities.Model1.ssdl|res://*/Entities.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=X;initial catalog=contoso2_db;persist security info=True;user id=X;password=X;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
但在桌面应用程序中使用此 WCF 时,我遇到了以下异常:
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client("BasicHttpsBinding_IService1");
ServiceReference1.QuestionSetInformation[] qss = service.GetQuestionSets("something");
FaultException`1 未处理 使用 T4 模板生成的代码 对于 Database First 和 Model First 开发可能无法正常工作 如果在 Code First 模式下使用。继续使用 Database First 或 Model 首先确保指定了实体框架连接字符串 在执行应用程序的配置文件中。要使用这些类, 从 Database First 或 Model First 生成的,带有代码 首先使用属性或 DbModelBuilder API,然后删除引发此问题的代码 例外。
我在网上调查了这个错误,每个人都说使用连接字符串有问题,但我认为情况不一样。我不知道如何解决它
【问题讨论】:
-
也许你对这个异常的来源感到困惑。它来自 服务器 而不是客户端。您只能在客户端捕获它。
-
但我不明白为什么只有在 WCF 投入生产时才抛出这个错误
标签: c# wcf entity-framework entity-framework-4.1 entity-framework-5