使用 MVC 实体框架将对象嵌套到单个类中

发布于 2024-10-08 13:55:46 字数 621 浏览 2 评论 0原文

我将 MVC 与实体框架一起使用,其中所有数据库表都成为对象。我对完整的面向对象编程还比较陌生,但我需要以下方面的建议。

我有一种数据类型(表),然后需要从其他表中提取大量信息来构建完整记录。

例如: 我有一个名为“建筑”的项目。每栋大楼都有一名“经理”和一组“楼层”。到目前为止,我正在创建一个包含所有这些对象的新类。参见下文:

   public BuildingComplete
   {
       public building _building;
       public manager _manager;
       public IEnumerable<floors> _floors;

       public BuildingComplete()
       {}
   }

然后我声明该对象并定义类外部的所有元素。这看起来相当混乱。理想情况下,我想创建一个“建筑”对象,并且在该对象内它将消失并在其中创建所有必要的元素。它可以做到这一点,因为“Building”对象包含用于查询其他数据表的所有链接 ID/信息。

我考虑创建“建筑物”的子项,但我不知道如何设置基础:“建筑物”从子项......或者即使这是正确的做法。

任何想法/指示将不胜感激。

I am using MVC with the Entity framework where all the database tables become objects. I am relatively new to full OO programming but I need advise on the following.

I have one data type (table) which then needs to pull in lots of information from other tables to construct a full record.

For example:
I have a item called "Building". Each building has one "manager" and a collection of "floors". So far I am creating a new class which contains all these objects. See below:

   public BuildingComplete
   {
       public building _building;
       public manager _manager;
       public IEnumerable<floors> _floors;

       public BuildingComplete()
       {}
   }

I'm then declaring this object and defining all the elements outside the class. This seems rather messy. Ideally I'd like to create a "building" object and within that object it will go away and create all the necessary elements within it. It can do this because the "Building" object contains all the linking id's / information to query the other data tables.

I looked at creating a child of "Building" but I don't know how to set the base: "Building" from the Child.....or even if this is the right thing to do.

Any ideas / pointers will be gratefully received.

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

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

发布评论

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

评论(1

悲歌长辞 2024-10-15 13:55:46

这是您的 OR/M 应该做的事情。如果没有的话,我认为它是没有用的。为此,我基本上爱上了 LLBLGen;但这需要花钱。就我而言,它是最好的,没有理由不购买它。

然而,我会惊讶地发现实体框架没有' 以某种方式为您执行此操作

-- 编辑:正如 @Lazarus 所指出的,大多数 ORM 将审查您在数据库中的关系(外键关系)并基于此生成层次结构。这意味着您需要进行此设置(如果还没有,请进行设置)。

This is something your OR/M should do. If it doesn't, in my opinion it is useless. I am basically in love with LLBLGen for this purpose; but it costs money. As far as I'm concerned, it's the best out there and there is no point not buying it.

I would, however, be surprised to learn that the Entity Framework didn't do this for you in some fashion.

-- Edit: As noted by @Lazarus, most ORMs will review the relatioinships you have in your DB (foreign key relationships) and generate a hierarchy based on that. This implies that you need to have this set up (if you don't already, do it).

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