ADO.NET 实体数据模型:自动生成的类

发布于 2024-08-03 11:27:16 字数 838 浏览 7 评论 0原文

我有一个如下所示的表结构:

Companies           Addresses
*********           *********
ID                  ID
AddressID           ...
BillingAddressID    ...

AddressID 和 BillingAddressID 是地址表中存在的外键。当我基于此表生成模型时,而不是在公司类中获取我期望得到的内容(AddressID、BillingAddressID)。我得到以下信息:

public Addresses Addresses { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> AddressesReference { .. }
public Addresses Addresses1 { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> Addresses1Reference { .. }

它似乎正在用 Addresses1 替换 BillingAddress(不太确定为什么会发生这种情况)。而且,只要我有外键,即我获取表而不是 ID,然后获取 TableReference,这似乎很常见。

我想我可以看到发生了什么,即它不会单独给我 ID,而是进行查找并找到 ID 引用的实际记录。但是,我不太确定 TableReference 字段的用途......

可以为我更好地解释一下吗?

提前致谢。

I have a table structure like the following:

Companies           Addresses
*********           *********
ID                  ID
AddressID           ...
BillingAddressID    ...

AddressID and BillingAddressID are foreign keys which are present in the Addresses table. When I generate my model based on this table instead of getting what I would expect to get (the AddressID, BillingAddressID) in the company class. I get the following:

public Addresses Addresses { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> AddressesReference { .. }
public Addresses Addresses1 { .. }
public global::System.Data.Objects.DataClasses.EntityReference<Addresses> Addresses1Reference { .. }

It seems to be replacing BillingAddress with Addresses1 (not quite sure why that's happening). Also this seems to be common wherever I have a foreign key i.e. instead of the ID I get Table then the TableReference.

I think I can see whats happening i.e. instead of giving me the ID alone, it will be doing a lookup and finding the actual record the ID refers to. However, I am not quite sure what the TableReference field is for....

Can explain this a little better for me?

Thanks in advance.

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

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

发布评论

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

评论(3

无敌元气妹 2024-08-10 11:27:16

关系在实体框架中以对象的形式表示,与实体的方式相同。即使您不打算直接在它们上进行大量工作,关系对象也是 EF 中的一等公民。 EF 创建 ObjectStateEntry 对象来跟踪关系的更改,就像它对实体所做的那样。

这就是为什么有两个参考。第一个 AddressesReference 是对关系对象的引用,而不是确切的实体,第二个 Addresses 是实际实体。

Peter Chan (链接),Julia Lerman 在她的《编程实体框架,第一版》一书中表示,了解关系在 EF 中的工作原理非常重要。他们还提到,这是开发人员开始使用 EF 时首先感到困惑的事情。

Relationships are represented as objects in Entity Framework, in the same manner as entities. Even if you are not going to work a lot directly on them, relationship object are first class citizens in EF. EF kreates ObjectStateEntry objects for tracking changes on relationships, just like it does it for entities.

That is why there are two references. First one, AddressesReference is a reference to the relationship object, not the exact entity, and second one Addresses is actual entity.

Peter Chan (link), and Julia Lerman in her book Programming Entity Framework, 1st Edition, say that understanding how relationship works in EF is very important. Also they mention that this is first thing that is confusing developer when they start using EF.

清醇 2024-08-10 11:27:16

外键被替换为外键指向的实体(集合)的引用。

因此,要向公司添加地址,您需要执行以下操作:

Address a = new Address();
// ... set variables for address here

currentCompany.Addresses = a;
// or, the other way round (for collections)
a.Companies.Add(currentCompany);

The foreign keys are replaced by a reference to the entity (collection) the foreign key points to.

So to add an address to a company you would do something like:

Address a = new Address();
// ... set variables for address here

currentCompany.Addresses = a;
// or, the other way round (for collections)
a.Companies.Add(currentCompany);
岁月流歌 2024-08-10 11:27:16

EF 在构建模型时使用表名称作为参考点,这就是您看到“Addresses”和“Addresses1”的原因。您可以以 GUI 格式打开实体模型,然后单击每个关联。这些关联可以重命名无论您喜欢什么,只需单击引用,查看映射,确保它是将“BillingAddressID”映射到“BillingAddressID”并将该引用重命名为“BillingAddress”

请注意,当前的“Addresses”引用可能是映射 。 “BillingAddressID”,因此您必须检查两个引用,

如果它也是一对一映射,则最好将“AddressID”的映射更改为“Address”而不是“Addresses”。

EF uses the table names as the reference point when it builds the model and this is why you see "Addresses" and Addresses1". You can open up the entity model in the GUI format and click on each of the associations. These can be renamed to whatever you like, just click on the reference, view the mapping, ensure it is the one that maps "BillingAddressID" to "BillingAddressID" and rename that reference to "BillingAddress".

Note the current "Addresses" reference may be the one mapping the "BillingAddressID" so you have to check both references.

It would probably be best to change the mapping for "AddressID" to be "Address" instead of "Addresses" if it is a one to one mapping as well.

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