【发布时间】:2014-05-22 19:44:30
【问题描述】:
我想从我的数据库中获取所有用户名,这样当一个新用户想要注册时,我可以检查该用户名是否已经存在于数据库中。我试图用这个 linq 表达式来做到这一点:
[AllowAnonymous]
public ActionResult Register()
{
var q = from t in _db.User
select t;
return View();
}
但是当我调试它 q 变成“无法评估儿童”。 我不知道为什么会出现这个错误,所以我只是发布了一些我认为可能与之相关的内容。
这就是我在 Register 方法之上定义 _db 的方式:
FantasySport _db = new FantasySport();
这里是 Fantasysport 数据库模型:
public class FantasySport : DbContext
{
public DbSet<UserProfile> User { get; set; }
public DbSet<Team> Team { get; set; }
public DbSet<TeamPlayer> TeamPlayer { get; set; }
public DbSet<Player> Player { get; set; }
public DbSet<Games> Games { get; set; }
public DbSet<League> League { get; set; }
}
这是 UserProfile 模型:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Country { get; set; }
public string City { get; set; }
public int Birthdate { get; set; }
public ICollection<Team> teams { get; set; }
}
【问题讨论】:
-
你能发布你得到的异常的堆栈跟踪(以及确切的异常消息和异常类型)吗?
-
我用您要求的数据在我的问题中添加了一张图片:)
-
这是预期行为:entityframework.codeplex.com/workitem/1995。其背后的原因与线程和通过调试防止副作用有关。
标签: c# linq entity-framework ef-code-first