简体   繁体   English

实体框架6将现有的复杂类型添加到键列

[英]Entity Framework 6 add existing complex type to key columns

I'm working on a project using EF 6 built using database-first approach. 我正在使用使用数据库优先方法构建的EF 6进行项目。 There are several tables that previously had the same 3 properties mapped to a complex type (used in several places throughout code). 有几个表以前具有将3个相同的属性映射到复杂类型(在整个代码中的多个地方使用)。 These properties were designated as "foreign key" columns, although their parent-child relationships were never defined in the database. 这些属性被指定为“外键”列,尽管它们的父子关系从未在数据库中定义。

Recently, a co-worker went through and formally defined the relationships between the FKs and their parent tables in SQL Server. 最近,一位同事审阅并正式定义了SQL Server中FK及其父表之间的关系。 I am now attempting to update the entity model (update from database), and the new associations did not appear. 我现在正在尝试更新实体模型(从数据库更新),并且没有出现新的关联。 I have resorted to removing the tables from the model and re-adding them. 我已采取从模型中删除表并重新添加它们的方法。 They now show the proper associations. 现在,它们显示了正确的关联。

However, the mapping to Complex Type is gone. 但是,到复杂类型的映射已消失。 When I try to add it back, the updated mappings destroy the associations on those properties (I get a model error when saving). 当我尝试将其重新添加时,更新的映射将破坏这些属性上的关联(保存时出现模型错误)。 I am aware that Complex Types do not support associations. 我知道,复杂类型不支持关联。

Is it recommended to first remove the associations for the columns prior to re-mapping them to the complex type? 是否建议先删除列的关联,然后再将其重新映射为复杂类型? Will EF observe the underlying key relationship even though the columns are not visible on the entity? 即使列在实体上不可见,EF也会观察基本的键关系吗?

While I would still like to get some feedback on my questions, here's what I did to solve my issues. 虽然我仍然想获得关于我的问题的一些反馈,但是这是我为解决问题所做的工作。

1) Removed the associations generated by EF when deleting and re-adding tables from the database. 1)从数据库中删除和重新添加表时,删除了EF生成的关联。 Note that these relationships are only for the foreign key fields which were to be replaced by the complex type. 请注意,这些关系仅适用于将由复杂类型替换的外键字段。

2) Followed the MS steps (found here ) to refactor the requisite key properties to a complex type 2)按照MS步骤(在此处找到)将必需的键属性重构为复杂类型

3) Optional: It was necessary for me to edit the EDMX directly as XML in order to remove some lingering associations that could not be repaired with the designer 3)可选:我有必要直接将EDMX编辑为XML,以删除一些设计师无法修复的缠绵关联。

So far, my model functions as intended. 到目前为止,我的模型已按预期运行。 I believe the FK relationships are observed during CRUD operations. 我相信在CRUD操作期间会观察到FK关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM