将对象添加到上下文并从关系中获取属性
我不久前编写了一些代码,首先使用实体框架代码将对象添加到我的表中。
Context.Reactions.Attach(reaction);
Context.SaveChanges();
之后,我使用名为 Profile 的关系来获取发布反应的用户的姓名。
reaction.Profile.FirstName + " " + reaction.Profile.LastName
现在,过了一会儿,当我尝试获取此人的姓名时,我在个人资料上得到了空引用。
那么为什么我不能添加一个对象,然后使用配置文件关系呢?
编辑: 我在配置文件关系属性上得到了空引用。所以reaction.Profile说那是空的。这是同一个实例。实体框架给了我一个反应 ID,因此 savechanges() 可以工作。 用于获取 FirstName 的行是直接在 SaveChanges 之后的行,因此无法正确处理上下文?
完整的代码:
var reaction = new Reaction()
{
Text = reactionText,
ProfileId = CurrentProfileId,
PostedOn = DateTime.Now
};
//Save changes to the backend
context.Reactions.Add(reaction);
context.SaveChanges();
return Json(new
{
Id = reaction.Id,
ReactionType = reactionType,
ReactionTypeId = reactionTypeId,
ReactionText = reaction.Text,
PostedOn = reaction.PostedOn.ToString("G"),
ProfileName = string.Format("{0} {1}", reaction.Profile.FirstName, reaction.Profile.LastName)
});
我什至尝试过使用它周围的代码。
using (var context = new SeeTingsContext())
{
context.Configuration.LazyLoadingEnabled = true;
}
I made a while ago some code that adds an object to my table with entity framework code first.
Context.Reactions.Attach(reaction);
Context.SaveChanges();
After that I used a relationship called Profile to get the name of the user that posted the reaction.
reaction.Profile.FirstName + " " + reaction.Profile.LastName
Now after a while, I get a nullreference on Profile when I try to get the name of the person.
So why can't I add an object and after that use the Profile relationship?
Edit:
I'm getting the nullreference on the Profile relation property. So reaction.Profile says that is null. It is the same instance. Entity Framework has given me an id for the reaction so the savechanges() works.
The line for getting the FirstName is the line directly after the SaveChanges so the context couldn't be disposed right?
Complete code:
var reaction = new Reaction()
{
Text = reactionText,
ProfileId = CurrentProfileId,
PostedOn = DateTime.Now
};
//Save changes to the backend
context.Reactions.Add(reaction);
context.SaveChanges();
return Json(new
{
Id = reaction.Id,
ReactionType = reactionType,
ReactionTypeId = reactionTypeId,
ReactionText = reaction.Text,
PostedOn = reaction.PostedOn.ToString("G"),
ProfileName = string.Format("{0} {1}", reaction.Profile.FirstName, reaction.Profile.LastName)
});
I even tryed with this code around it.
using (var context = new SeeTingsContext())
{
context.Configuration.LazyLoadingEnabled = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我
过了一会儿
阅读时,我不太明白你的意思。这是否意味着您先获取关系对象,然后它突然变成 null?我的猜测是:
LazyLoadingEnabled
设置为 true。更新:根据评论,尝试显式加载关系并查看是否可以获得配置文件:
如果您无法使用这种方式获取配置文件,我想您的映射有问题。
I didn't quite get you when I read
after a while
. Does this mean you get the relation object first, and then it suddenly becomes null?My guess is:
LazyLoadingEnabled
to true for the context you're working on.Update: Based on the comments, try to load the relation explicitly and see if you can get the profile:
If you cannot get profile using this way, I suppose there's something wrong with your mapping.