2015-11-07 62 views
0

我在我的解決方案中有一個WCF項目和一個EF項目,我打電話給我的EF來獲得一個對象,而是我得到null返回。我不確定爲什麼,因爲我的SQL Server數據庫中有數據,如果這對數據線索有任何影響,我使用數據庫優先建模。WCF方法不返回EF的結果

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 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> 
    <connectionStrings><add name="InventoryManagerEntities" connectionString="metadata=res://*/InventoryManagerDBModels.csdl|res://*/InventoryManagerDBModels.ssdl|res://*/InventoryManagerDBModels.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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=&quot;data source=JUNIORLABOLD4A3\SQLEXPRESS;initial catalog=InventoryManager;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 

回答

0

你在你的代碼的兩個錯誤。變量user永遠不會被分配,謂詞user.usr_Password == password是錯誤的。

public User GetUser(string username, string password) 
{ 
    User user = new User(); // <= user is only initialized 
    try 
    { 
     if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) 
     { 
      throw new Exception("Invalid username and password"); 
     } 
     var context = new InventoryManagerEntities(); 
     var userContext = // <= should be "user", not "var userContext" 
      (from usr in context.Users 
       where usr.usr_Username == username 
        && user.usr_Password == password // <= should be usr.usr_Password 
       select usr).FirstOrDefault(); 

    } 
    catch (Exception) 
    { 
     throw; 
    } 
    return user; 
} 

所以,刪除所有噪音後:

if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) 
{ 
    throw new Exception("Invalid username and password"); 
} 
var context = new InventoryManagerEntities(); 
return (from usr in context.Users 
     where usr.usr_Username == username 
      && usr.usr_Password == password 
     select usr).FirstOrDefault();