EF / LINQ:找到()操作 [英] EF/LINQ : Find() operation

查看:191
本文介绍了EF / LINQ:找到()操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取当前用户的数据行。但是,当我执行此code

I'm trying to get the current user data row. But, when I execute this code

MNDataContext _db = new MNDataContext();
var nm = User.Identity.Name;
var id = (int)Membership.GetUser(User.Identity.Name).ProviderUserKey;
var slct = _db.Utilisateurs.Find((int)Membership.GetUser(User.Identity.Name).ProviderUserKey);

我收到作为对 VAR SLCT 行的值。这是奇怪,因为调试表明,纳米和身份证有其正确的价值观,但最后一个是没有得到正确的对象值。

I'm getting null as a value on the var slct line. This is weird because the debugger indicates that nm and id have their right values but the last one isn't getting the right object value.

MNDataContext类

MNDataContext class

public class MNDataContext: DbContext
{
    public DbSet<UtilisateurModel> Utilisateurs { get; set; }
}

Utilisateur的主键:

Utilisateur's primary key:

[Key]         
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]         
[Display(Name = "Identifiant")]         
public int UserId { get; set; }

任何高招,好吗?

Any brilliant idea, please?

非常感谢!

推荐答案

只是胡乱猜测......你是做一个int找到它期望的对象。尝试这样的事情

Just a wild guess... You are doing a find of int where it expects an object. Try something like this

MNDataContext _db = new MonitoringNDataContext();
var nm = User.Identity.Name;
var id = (int)Membership.GetUser(User.Identity.Name).ProviderUserKey;
UtilisateurModel slct = _db.Utilisateurs.Find(x=>x.Id=id);

这篇关于EF / LINQ:找到()操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆