我在C#中遇到了实体框架的问题.我有2个实体,User和UserRole.他们是关系用户* - > 1 UserRole
每当我在函数中使用此查询时:
User user = context.User.Where(i => i.id == id).FirstOrDefault();
return user.UserRole.accessLevel;
Run Code Online (Sandbox Code Playgroud)
查询返回用户,但UserRole为null.User表具有与UserRole的id相关的roleId,以及在调试正确时roleId的值,尽管UserRole实体为null.这很奇怪,因为它从未发生过......
我已经确定我在模型和数据库中的关系是正确的.我有正确的行添加到数据库.
编辑:
对不起,我应该提到我使用自定义可测试数据库控制器:
public class DBController : IUnitOfWork
{
readonly ObjectContext context;
const string ConnectionStringName = "MarketPlaceDBEntities";
public DBController()
{
var connectionString =
ConfigurationManager
.ConnectionStrings[ConnectionStringName]
.ConnectionString;
context = new ObjectContext(connectionString);
}
public void Commit()
{
context.SaveChanges();
}
public IObjectSet<Category> Category
{
get { return context.CreateObjectSet<Category>(); }
}
public IObjectSet<ItemComment> ItemComment
{
get { return context.CreateObjectSet<ItemComment>(); }
}
public IObjectSet<ItemRating> ItemRating
{
get { …Run Code Online (Sandbox Code Playgroud) 我不明白为什么这段代码不能正常工作:
program selection;
var
n : integer;
begin
readln(n);
if (n in [100..1000]) then writeln('Selected!');
readln;
end.
Run Code Online (Sandbox Code Playgroud)
这对我来说很好,值在1到233之间,如果我输入233或更多,则不会执行writeln .. 这很奇怪.我也尝试过其他值,结果或多或少相同,唯一不同的是它失败的值.