实体框架上下文有0个结果

时间:2015-11-06 18:27:29

标签: c# asp.net-mvc entity-framework wcf dbcontext

我正在尝试调用我的实体框架模型,并将结果返回给我的WCF服务。

WCF服务方法

public User GetUser(string username, string password)
{
    User userProfile = null;
    try 
    {
        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
        {
            throw new Exception("Username and password is null or empty");
        }
        var context = new InventoryManagerEntities();
        var userContext = (from user in context.Users
            where
                user.usr_Username.Equals(username, StringComparison.CurrentCultureIgnoreCase) &&
                user.usr_Password.Equals(password, StringComparison.CurrentCultureIgnoreCase)
            select user).
            FirstOrDefault();

    }
    catch (Exception exception)
    {
        throw exception;
    }
    return userProfile;
}

这个问题是我的上下文在每个Entity对象中都没有数据。所以例如用户有0个结果但是在SQL服务器中我可以看到我的表中有数据。

顺便说一下,我的实体项目在同一个解决方案的不同项目中,我不确定是否与它有关。

EF项目连接字符串 我也在我的WCF项目中使用相同的连接字符串。 Sql Server管理工具中的数据库名称为InventoryManager

    <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>

1 个答案:

答案 0 :(得分:0)

您必须将ConnectionString添加到每个项目中。在Web.config或App.config中。

如果您的项目没有这两个文件中的一个,只需创建它。但我确信没有加载任何结果的项目与实体项目有不同的connectionString。

你必须知道MVC-Projects主要是自动创建一个connectionString。检查一下!