【发布时间】:2025-12-10 09:00:02
【问题描述】:
我的解决方案中有一个 WCF 项目和一个 EF 项目,我正在调用我的 EF 以获取一个对象,但我得到了 null 返回。我不知道为什么,因为我的 SQL Server 数据库中确实有数据,如果这对线索有任何影响,我将使用数据库优先建模。
我在 SQL Server 管理工具中的每个表都有数据,但是当我向 GetUser 方法添加断点并单步执行时,每个表对象的计数为 0,最后我返回一个User 对象,它是null
WCF 方法
public User GetUser(string username, string password)
{
User user = new User();
try
{
if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
{
throw new Exception("Invalid username and password");
}
var context = new InventoryManagerEntities();
var userContext =
(from usr in context.Users
where usr.usr_Username == username && user.usr_Password == password
select usr).FirstOrDefault();
}
catch (Exception)
{
throw;
}
return user;
}
WCF 应用程序配置
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings><add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerDBModels.csdl|res://*/InventoryManagerDBModels.ssdl|res://*/InventoryManagerDBModels.msl;provider=System.Data.SqlClient;provider connection string="data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /><add name="InventoryManagerEntities1" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="InventoryManager.Services.UserService">
<endpoint address="" binding="basicHttpBinding" contract="InventoryManager.Services.IUserServices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/InventoryManager.Services/UserService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True" />
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
EF 类库 App.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="InventoryManagerEntities"
connectionString="metadata=res://*/InventoryManagerDBModels.csdl|res://*/InventoryManagerDBModels.ssdl|res://*/InventoryManagerDBModels.msl;provider=System.Data.SqlClient;provider connection string="data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
【问题讨论】:
标签: c# asp.net-mvc entity-framework wcf app-config