将对象添加到上下文并从关系中获取属性

发布于 2024-12-15 10:26:23 字数 1636 浏览 0 评论 0原文

我不久前编写了一些代码,首先使用实体​​框架代码将对象添加到我的表中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-12-22 10:26:23

当我过了一会儿阅读时,我不太明白你的意思。这是否意味着您先获取关系对象,然后它突然变成 null?
我的猜测是:

  • 您尚未将您正在处理的上下文的 LazyLoadingEnabled 设置为 true。
  • 您的上下文已被处理,因此您无法使用它来获取关系。请注意,当上下文仍然存在时,您可以使用延迟加载。

更新:根据评论,尝试显式加载关系并查看是否可以获得配置文件:

if (!context.Entry(reaction).Reference(r => r.Profile).IsLoaded)
{
    context.Entry(reaction).Reference(r => r.Profile).Load();
}  

如果您无法使用这种方式获取配置文件,我想您的映射有问题。

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:

  • You have not set LazyLoadingEnabled to true for the context you're working on.
  • Your context is disposed and thus you cannot use it to get the relationship. Note that you can use Lazy loading while your context still exists.

Update: Based on the comments, try to load the relation explicitly and see if you can get the profile:

if (!context.Entry(reaction).Reference(r => r.Profile).IsLoaded)
{
    context.Entry(reaction).Reference(r => r.Profile).Load();
}  

If you cannot get profile using this way, I suppose there's something wrong with your mapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文