【问题标题】:Entity framework context has 0 results实体框架上下文有 0 个结果
【发布时间】:2015-11-06 18:27:29
【问题描述】:

我正在尝试调用我的实体框架模型并将结果返回给我的 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;
}

这个问题是我的上下文在每个实体对象中都没有数据。因此,例如用户有 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>

【问题讨论】:

  • 这可能是您的数据库上下文配置的问题。如果您不按用户名和密码过滤会发生什么,结果是空的吗?如果这是真的,那么你的映射是错误的,错误是在实体项目配置上。
  • 我认为这不会改变任何东西,我也尝试过使用 user.usr_Password == 密码语法并且没有返回任何内容,问题是上下文对我在 EF 中的所有表的计数均为 0虽然数据库本身有数据@LuisBecerril
  • 你为什么要实现自己的(糟糕的)身份验证服务?您可以使用其中 4 个来进行正确的密码散列和加盐处理。最重要的是,您正在以不区分大小写的方式比较密码?认真的吗?
  • @RobertMcKee 这不是现在的重点......我只是想用 wcf 提供的输入返回数据
  • @ifelabolz 抱歉,我更新了我的评论

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


【解决方案1】:

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

如果您的项目没有这两个文件之一,只需创建它。但我确信不加载任何结果的项目具有与实体项目不同的连接字符串。

您必须知道 MVC 项目大多会自动创建连接字符串。检查一下!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多