ASP.NET MVC/EF:我应该如何处理新实体的导航属性?

发布于 2024-09-25 12:47:48 字数 903 浏览 7 评论 0原文

我的特定应用程序是使用实体框架的 ASP.NET MVC 应用程序。

我有一个对象 Entity0,其中包含对 Entity1 的引用 (* to 1)...

public class Entity_0
{
    public int ID { get; set; }
    public int Entity_1_ID { get; set; }
    public Entity_1 Entity_1 { get; set; }
}

我在 Entity_0 中也有一些逻辑,但是此逻辑要求 Entity_1 不为空...

public Entity_2 GetEntity_2()
{
    return new Entity_2() { Number = Entity_1.Value * 10 };
}

当通过模型绑定创建 Entity_0 的新实例时在控制器中,设置了 Entity_1_ID 属性,但 Entity_1 导航属性保持为空,直到将其保存到数据库中为止。

我需要调用 GetEntity_2() 并将 Entity_0 和 Entity_2 作为单个工作单元保存到数据库中。现在,这意味着我必须在调用该函数之前手动从存储库中检索 Entity_1 并将其分配给 Entity_0。

我考虑的另一个选项是将 Entity_1 作为 GetEntity_2 方法的参数,但是任何其他时候调用该方法时 Entity_1 都不会为 null,因此需要参数是多余的。

显然,现在一切都正常,但我的问题是,我是否违反了一些 OOP 设计规则,因为 a) 拥有一个可能引发空引用异常的方法,或者 b) 指定一个只应在某些情况下使用的方法参数时间。

必须手动分配导航属性真的很困扰我。我希望你们中的一些人对我如何改进这一点提出一些建议。

谢谢!

My particular application is an ASP.NET MVC app using Entity Framework.

I have an object Entity0 which contains a reference (* to 1) to Entity1...

public class Entity_0
{
    public int ID { get; set; }
    public int Entity_1_ID { get; set; }
    public Entity_1 Entity_1 { get; set; }
}

I also have some logic in Entity_0, however this logic requires that Entity_1 not be null...

public Entity_2 GetEntity_2()
{
    return new Entity_2() { Number = Entity_1.Value * 10 };
}

When a new instance of Entity_0 is created via model binding in the controller, the Entity_1_ID property is set, however the Entity_1 navigation property remains null until it is persisted to the database.

I need to call GetEntity_2() and save Entity_0 and Entity_2 to the database as a single unit of work. Right now this means that I have to manually retrieve Entity_1 out of the repository and assign it to Entity_0 before calling the function.

The other option I considered would be to make Entity_1 a parameter of the GetEntity_2 method, however any other time that method is called Entity_1 will not be null so it would be redundant to require a parameter.

So obviously everything is working right now, but my question is whether or not I am violating some OOP design rule by either a) having a method that could throw a null reference exception or b) specifying a method parameter that should only be used some of the time.

Having to manually assign the navigation property is really bugging me. I'm hoping some of you have some suggestions on how I can improve this.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

魂归处 2024-10-02 12:47:48

啊,这比我想象的要容易得多!

将对象添加到上下文后,将填充导航属性 Entity_1。我的印象是它是空的,直到我调用 SaveChanges() 并将其保存到数据库中。

这使得调用代码变得很容易,如下所示......

var entity_1 = Entity_1CreateViewModel.Entity_1;
entitiesRepository.Add(entity_1);
entitiesRepository.Add(entity_1.GetEntity_2());
entitiesRepository.Save();

Ah this turned out to be way easier than I had imagined!

The navigation property Entity_1 is populated after adding the object to the context. I was under the impression that it was null until I called SaveChanges() and persisted it to the database.

This makes it easy to call the code as follows...

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