EF4:创建 0..1 到多关联

发布于 2024-11-04 08:04:07 字数 1005 浏览 6 评论 0原文

我正在尝试添加一个我原来没有的关联。我意识到两个表在技术上是相关的,并且某些导航属性可能会简化我原本必须手动执行的操作。这些表及其键如下所示:

Import
Primary Key:
   Number : Int32
   Date : DateTime

Hour
Primary Key:
   DepartmentID : Int32
   UserNumber : Int32
   Date : DateTime

该关联名为 ImportHour。 Import.Number 映射到 Hour.UserNumber,Import.Date 映射到 Hour.Date。我正在尝试添加一个关联,该关联在“导入”上为 0..1,在“小时”上为 *,具有导航属性,并且没有其他外键。当我这样做时,设计者告诉我关联没有映射。如果我随后生成 DDL,它将创建新字段 Hours.Import_Date 和 Hours.Import_Number (Hours 是 Hour 实体的实际数据库表名称)。如果我手动映射字段,最终会出现以下错误:

Error 3021: Problem in mapping fragments starting at line 332:
Each of the following columns in table Hours is mapped to multiple conceptual side properties:
Hours.Date is mapped to <ImportHour.Hour.Date, ImportHour.Import.Date>
Hours.UserNumber is mapped to <ImportHour.Hour.UserNumber, ImportHour.Import.Number>*

我不太确定发生了什么,并且我认为我对“映射”过程的理解不够好,无法弄清楚这一点。看起来似乎它想要一个五重密钥,而不是意识到一个密钥映射到另一个密钥。我查看了其他一对多关联,它们甚至没有表映射;我认为它们有引用约束,但显然不能有 0..1 到多个关联的引用约束。

I am trying to add an association that I didn't have originally. I realized that two tables were technically related, and that some navigation properties might simplify what I would have otherwise had to do manually. The tables and their keys look like this:

Import
Primary Key:
   Number : Int32
   Date : DateTime

Hour
Primary Key:
   DepartmentID : Int32
   UserNumber : Int32
   Date : DateTime

The association is named ImportHour. Import.Number maps to Hour.UserNumber, and Import.Date maps to Hour.Date. I am trying to add an association that is 0..1 on Import, and * on Hour with navigation properties and no additional foreign keys. When I do this, the designer tells me that the association is not mapped. If I then generate the DDL, it creates new fields Hours.Import_Date and Hours.Import_Number (Hours is the actual database table name for the Hour entity). If I manually map the fields, I end up with the following error:

Error 3021: Problem in mapping fragments starting at line 332:
Each of the following columns in table Hours is mapped to multiple conceptual side properties:
Hours.Date is mapped to <ImportHour.Hour.Date, ImportHour.Import.Date>
Hours.UserNumber is mapped to <ImportHour.Hour.UserNumber, ImportHour.Import.Number>*

I am not really sure what is happening, and I don't think I understand the 'mapping' process well enough to figure this out. It almost seems as if it wants a quintuple key, instead of realizing that the one key maps to the other. I look at my other one-to-many associations, and they do not even have table mappings; I think they have referential constraints instead, but you obviously can't have a referential constraint with a 0..1 to many association.

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

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

发布评论

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

评论(3

夏日落 2024-11-11 08:04:07

有两种方法可以定义关系,但在您的情况下,您必须使用外键关联。这意味着一旦您在实体模型中绘制关联,您必须 选择它并定义引用约束

Import 上不能有 0..1,因为在这种情况下,Hour 中的 UserNumberDate 必须可为空。这就是这种关系的含义。如果不存在主体实体 (Import),从属实体 (Hour) 中的 FK 属性将为 null。

顺便提一句。在主键中使用 DateTime 不建议< /a>.

There are two ways to define relation but in your case you must use the Foreign key association. It means that once you draw association in entity model you must select it and define referential constraints.

You cannot have 0..1 on Import because in such case UserNumber and Date in Hour must be nullable. That is what that relation mean. If no principal entity exists (Import) FK properties in dependent entity (Hour) will be null.

Btw. using DateTime in primary key is not recommended.

恋竹姑娘 2024-11-11 08:04:07

据我从使用过的其他数据库来看,这里的问题似乎是 EF 模型需要数据库中已存在外键。虽然我似乎无法让 EF 生成一个,但如果它已经存在,它会接受一个。 (与我在问题中所说的相反,您可以对 0..1 到多个(可为空)外键有引用约束)。

As far as I can tell from other databases I have since used, the issue here seems to be that the EF model requires a foreign key to already exist in the database. While I cannot seem to get EF to generate one, it will accept one if it already exists. (Contrary to what I said in the question, you can have a referential constraint on a 0..1 to many (nullable) foreign key).

夏末 2024-11-11 08:04:07

@Sahuagin,这可能是在您提出问题之后很长时间,但是您在添加关联、删除设计器中的标量属性之后是否尝试过 - 例如在创建 ImportHour 关联之后,从小时实体中删除 hour.usernumber 和 hour.date 。
这样建立的独立关联就是你实体之间的唯一关系——这就是独立关联的意义

@Sahuagin this may be long after your question but did you try after adding the association, deleting the scalar property in the designer -- example after creating the ImportHour association, delete the hour.usernumber and hour.date from your hour entity.
this way the independent association established this way is the only relationship between yuor entities - thats the meaning of independent association

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