简体   繁体   English

外键冲突 ef 核心脚手架

[英]Foreign key conflict ef core scaffolding

The foreign key {'ClassExtId'} cannot be added to the entity type 'ClassInt' because a foreign key on the same properties already exists on entity type 'Instrument' and also targets the key {'ClassExtId'} on 'ClassExt'.无法将外键 {'ClassExtId'} 添加到实体类型“ClassInt”,因为实体类型“Instrument”上已存在相同属性的外键,并且还针对“ClassExt”上的键 {'ClassExtId'}。

Let's say I have ClassFoo & ClassBar inheriting from ClassInt.假设我有从 ClassInt 继承的 ClassFoo 和 ClassBar。 Each one of them have a reference to the same column ClassExtId.它们中的每一个都有对同一列 ClassExtId 的引用。 Why is the entity framework core scaffolding (v3) complaining?为什么实体框架核心脚手架(v3)抱怨?

my case is different but i got the same error too when i was trying to scaffold my existing old database.我的情况不同,但是当我尝试搭建现有的旧数据库时,我也遇到了同样的错误。 i'm, posting this answer just in case someone also had the same problem with me.我是,发布这个答案以防万一有人也和我有同样的问题。

it's turn out that in my old database, some table have so many duplicate foreign key created by previous developer.事实证明,在我的旧数据库中,某些表有很多以前开发人员创建的重复外键。 you can check this in your sql server (i use sql server developer edition 2019)你可以在你的 sql 服务器中检查这个(我使用 sql 服务器开发者版 2019)

  1. In Object Explorer, right-click the table that will be on the foreign-key side of the relationship and click Design.在 Object 资源管理器中,右键单击将位于关系外键侧的表,然后单击设计。
    The table opens in Table Designer .该表在表设计器中打开。

  2. From the Table Designer menu, click Relationships .从表设计器菜单中,单击关系 you'll see all the foreign key defined for that table, in my case it was applicationID that duplicate.您将看到为该表定义的所有外键,在我的情况下,它是重复的 applicationID。 clean it up:清理: 在此处输入图像描述

  3. go to your table > expand it and go to Keys . go 到您的表>展开它和 go 到Keys delete the duplicate key there:删除那里的重复键: 在此处输入图像描述

  4. run your dotnet ef dbcontext scaffold command and good luck!运行你的dotnet ef dbcontext scaffold命令,祝你好运!

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

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