- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有相互引用的模型:
public class Dept
{
[Key]
public int DeptId { get; set; }
[ForeignKey("ManagerId")]
public Emp Manager { get; set; }
public int? ManagerId { get; set; }
public string DeptName { get; set; }
}
public class Emp
{
[Key]
public int EmpId { get; set; }
[Required]
[ForeignKey("DeptId")]
public Dept Dept { get; set; }
public int DeptId { get; set; }
public string Name { get; set; }
}
当我调用 Add-Migration 时,出现错误:
The ForeignKeyAttribute on property 'Manager' on type 'App.Dept' is not valid. The foreign key name 'ManagerId' was not found on the dependent type 'App.Emp'. The Name value should be a comma separated list of foreign key property names.
我应该如何使用这些表创建迁移?
更新:隐式可选管理器没有解决问题:
modelBuilder.Entity<Emp>().HasRequired(_ => _.Dept).WithOptional(_ => _.Manager);
UPD2: Dept:Emp 关系是 1:0..1
UPD3:也许另一个关系会被添加到 Dept 模型中,但它也是 1:0..1:
[ForeignKey("ManagerId")]
public Emp CTO { get; set; }
public int? CTOId { get; set; }
这不是一对多关系:一个部门有零个或一个经理,零个或一个 CTO。目前我只有一个关系,但我想将该字段命名为 ManagerId,而不是 EmpId。
UPD4:我的问题开头的架构与两个主/外键关系(Dept.DeptId/Emp.DeptId、Emp.EmpId/Dept.ManagerId)在普通 SQL 中工作。我知道使用附加表或不使用外键的解决方法,但我需要一个答案,说明如何在上面创建工作模式或为什么它在 EF 中不起作用。
最佳答案
您主要有三种配置 1-1 关系的方法(您的错误是第 3 种解释的情况)。
复杂类型
第一种方法是只有一个表并使用复杂类型。选择此配置会影响性能(通常,整体性能优于其他配置,但这取决于记录大小以及同时拥有这两条记录的次数)。
在您的情况下,您只需要用 ComplexType
属性标记其中一个实体
public class Dept
{
[Key]
public int DeptId { get; set; }
public Emp Manager { get; set; }
public string DeptName { get; set; }
}
[ComplexType]
public class Emp
{
public int EmpId { get; set; } // You can still have this property but it will not be a primary key
public string Name { get; set; }
}
对于这个模型,这是创建的表
CREATE TABLE [Depts] (
[DeptId] int not null identity(1,1)
, [Manager_EmpId] int not null
, [Manager_Name] text null
, [DeptName] text null
);
标准外键
第二种方法是使用标准外键。该模型可以在两个类上都具有导航属性,有 2 个带有 independent 主键的表,但只有 1 个表具有另一个表的外键(您在问题中写过此配置)。您获得此配置覆盖 OnModelCreating
。使用这种方式,您可以使用流畅的 API 进行多种配置。主要选项是 EF 应该在哪里插入外键。在每个配置中你都必须有 Map 方法(我在第三种方式中解释了没有 Map 方法会发生什么)
模型总是这样
public class Dept
{
[Key]
public int DeptId { get; set; }
public Emp Manager { get; set; }
public string DeptName { get; set; }
}
public class Emp
{
[Key]
public int EmpId { get; set; }
public Dept Department { get; set; }
public string Name { get; set; }
}
WithRequiredPrincipal (1-1)
From MSDN: Configures the relationship to be required:required without a navigation property on the other side of the relationship. The entity type being > configured will be the principal in the relationship. The entity type that the relationship targets will be the dependent and contain a foreign > key to the principal.
modelBuilder.Entity<Dept>()
.HasRequired(_ => _.Manager)
.WithRequiredPrincipal(_ => _.Department)
.Map(_ => _.MapKey("DepartmentId"));
这是生成的DDL
ExecuteNonQuery==========
CREATE TABLE [Depts] (
[DeptId] int not null identity(1,1)
, [DeptName] text null
);
ALTER TABLE [Depts] ADD CONSTRAINT [PK_Depts_c0491d33] PRIMARY KEY ([DeptId])
ExecuteNonQuery==========
CREATE TABLE [Emps] (
[EmpId] int not null identity(1,1)
, [Name] text null
, [DepartmentId] int not null
);
ALTER TABLE [Emps] ADD CONSTRAINT [PK_Emps_c0491d33] PRIMARY KEY ([EmpId])
ExecuteNonQuery==========
CREATE INDEX [IX_DepartmentId] ON [Emps] ([DepartmentId])
ExecuteNonQuery==========
ALTER TABLE [Emps] ADD CONSTRAINT [FK_Emps_Depts_DepartmentId] FOREIGN KEY ([DepartmentId]) REFERENCES [Depts] ([DeptId])
WithRequiredDependent (1-1)
From MSDN: Configures the relationship to be required:required without a navigation property on the other side of the relationship. [For me is not clear this explanation, anyway, for the real behaviour see below]
modelBuilder.Entity<Dept>()
.HasRequired(_ => _.Manager)
.WithRequiredDependent(_ => _.Department)
.Map(_ => _.MapKey("EmpId"));
这是生成的DDL
ExecuteNonQuery==========
CREATE TABLE [Depts] (
[DeptId] int not null identity(1,1)
, [DeptName] text null
, [EmpId] int not null
);
ALTER TABLE [Depts] ADD CONSTRAINT [PK_Depts_bebceea2] PRIMARY KEY ([DeptId])
ExecuteNonQuery==========
CREATE TABLE [Emps] (
[EmpId] int not null identity(1,1)
, [Name] text null
);
ALTER TABLE [Emps] ADD CONSTRAINT [PK_Emps_bebceea2] PRIMARY KEY ([EmpId])
ExecuteNonQuery==========
CREATE INDEX [IX_EmpId] ON [Depts] ([EmpId])
ExecuteNonQuery==========
ALTER TABLE [Depts] ADD CONSTRAINT [FK_Depts_Emps_EmpId] FOREIGN KEY ([EmpId]) REFERENCES [Emps] ([EmpId])
WithOptional (1-0..1)
modelBuilder.Entity<Dept>()
.HasRequired(_ => _.Manager)
.WithOptional(_ => _.Department)
.Map(_ => _.MapKey("ManagerId"));
这是生成的DDL
ExecuteNonQuery==========
CREATE TABLE [Depts] (
[DeptId] int not null identity(1,1)
, [DeptName] text null
, [ManagerId] int not null
);
ALTER TABLE [Depts] ADD CONSTRAINT [PK_Depts_ee5245bb] PRIMARY KEY ([DeptId])
ExecuteNonQuery==========
CREATE TABLE [Emps] (
[EmpId] int not null identity(1,1)
, [Name] text null
);
ALTER TABLE [Emps] ADD CONSTRAINT [PK_Emps_ee5245bb] PRIMARY KEY ([EmpId])
ExecuteNonQuery==========
CREATE INDEX [IX_ManagerId] ON [Depts] ([ManagerId])
ExecuteNonQuery==========
ALTER TABLE [Depts] ADD CONSTRAINT [FK_Depts_Emps_ManagerId] FOREIGN KEY ([ManagerId]) REFERENCES [Emps] ([EmpId])
您可以使用其他方法获得类似的配置。我没有在这里展示每个示例,但我们可以混合这些配置
HasOptional
/WithRequired
HasOptional
/WithOptionalDependent
HasOptional
/WithOptionalPrincipal
EF默认0..1-1 1-0..1 1-1配置
这就是 EF 解释您的配置的方式。在这种情况下,EF 生成 2 个具有相关 主键的表。在一张表上有一个独立的主键(在您的情况下为 identity(1,1)),在另一张表上您有一个主键,它也是外键。这是默认配置。 这是在两个表上都有外键的唯一方法(不是 2 个约束,没有办法有 2 个循环约束,见下文)
modelBuilder.Entity<Dept>()
.HasRequired(_ => _.Manager)
.WithRequiredPrincipal(_ => _.Department);
这是生成的DDL
ExecuteNonQuery==========
CREATE TABLE [Depts] (
[DeptId] int not null identity(1,1)
, [DeptName] text null
);
ALTER TABLE [Depts] ADD CONSTRAINT [PK_Depts_b91ed7c4] PRIMARY KEY ([DeptId])
ExecuteNonQuery==========
CREATE TABLE [Emps] (
[EmpId] int not null
, [Name] text null
);
ALTER TABLE [Emps] ADD CONSTRAINT [PK_Emps_b91ed7c4] PRIMARY KEY ([EmpId])
ExecuteNonQuery==========
CREATE INDEX [IX_EmpId] ON [Emps] ([EmpId])
ExecuteNonQuery==========
ALTER TABLE [Emps] ADD CONSTRAINT [FK_Emps_Depts_EmpId] FOREIGN KEY ([EmpId]) REFERENCES [Depts] ([DeptId])
这应该是一对一的关系,但如果我们看起来更好,就会缺少一个约束。 Dept 表的主键应该是第二个表的外键。为什么 EF 没有插入该约束?因为我们总是会违反约束,所以我们不能在表中插入记录(在事务中也可能违反引用键约束)。
修改配置为HasRequired
/WithRequiredDependent
我们得到的主键独立的表就是Emps表
modelBuilder.Entity<Dept>()
.HasRequired(_ => _.Manager)
.WithRequiredDependent(_ => _.Department);
这是生成的DDL
ExecuteNonQuery==========
CREATE TABLE [Depts] (
[DeptId] int not null
, [DeptName] text null
);
ALTER TABLE [Depts] ADD CONSTRAINT [PK_Depts_58ab8622] PRIMARY KEY ([DeptId])
ExecuteNonQuery==========
CREATE TABLE [Emps] (
[EmpId] int not null identity(1,1)
, [Name] text null
);
ALTER TABLE [Emps] ADD CONSTRAINT [PK_Emps_58ab8622] PRIMARY KEY ([EmpId])
ExecuteNonQuery==========
CREATE INDEX [IX_DeptId] ON [Depts] ([DeptId])
ExecuteNonQuery==========
ALTER TABLE [Depts] ADD CONSTRAINT [FK_Depts_Emps_DeptId] FOREIGN KEY ([DeptId]) REFERENCES [Emps] ([EmpId])
您可以使用其他方法获得类似的配置。我没有在这里展示每个示例,但我们可以混合这些配置
HasOptional
/WithRequired
HasOptional
/WithOptionalDependent
HasOptional
/WithOptionalPrincipal
关于entity-framework - 当实体具有与外键的交叉引用时的代码优先迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492757/
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicates: What is a framework? What does it do? Why do we need a f
我在按照 http://msdn.microsoft.com/en-us/data/jj591621.aspx 处的 Microsoft Data Developer 过程启用代码优先迁移时遇到了一些
我正在从 迁移项目 Entity Framework 4.3 在 .net 4 上运行到 Entity Framework 5 在 .net 4.5 上运行。在不做任何更改的情况下,当我尝试运行该项目
我正在使用 Entity Framework 6 并使用 EntityFramework Extended 来执行一些批量更新和批量删除。批量更新和批量删除工作正常,但我还需要知道更新/删除的实体(即
我在实体上添加了一个列,然后从模型中生成数据库或构建解决方案,然后收到一条消息,提示我刚添加的新列未映射。该数据库以前是从模型创建的,没有错误。 当我右键单击Entity并选择Table Mappin
每次我尝试运行我的代码时都会崩溃,因为我尝试启动函数以调用 SDK 的任何部分。 我在构建过程中包含了 FoundationSDK: 并且我在头文件中包含了对 SDK 的引用: 但是每次我运行这个,我
我以前能够毫无问题地提交我的申请。我的工作流程中唯一改变的部分是使用 Sourcetree。在对以下框架进行更新后,我在提交到 iOS App Store 时收到此警告。我还收到一封电子邮件,其中包含
假设我为 Asp.NET Web 应用程序安装了 .NET Framework 2.0、3.0、3.5。 我意识到 Framework 3.0 和 3.5 只是 Framework 2 的扩展,不太清
是否有 SaveChanges 事件在保存更改后但在更新更改跟踪器之前触发? 我正在使用 EF 6。 我需要在某个实体的状态发生变化时执行任务。 我已经覆盖了 SaveChanges 来设置它。我可以
我正在使用一个现有的数据库,并且我已经将其中一个表映射为一个实体(因为我需要映射一个外键)。 因此,在初始化此数据库时,我希望 EF 忽略此实体,因为它已经存在。 我该怎么做? 最佳答案 您应该使用
我有 3 个表需要与 Entity Framework 进行映射,但我不确定解决此问题的正确方法。这是我的 3 个实体: public class User { [Key] public
我首先使用 VS 2010 和 Entity Framework 代码(版本 6)。我有两个实体,每个实体都在自己的上下文中,我想在它们之间创建一对多关系。 上下文 1 具有以下实体: public
我知道 EF 在 CodePlex 上是开源的,但我没有看到当前发布的 5.0 版本的分支。我在哪里可以得到这个源代码? 最佳答案 没有。他们只开源了 post 5 版本。第一次签到可能足够接近,但再
我们目前有一个数据库很大的系统,存储过程既用于CUD又用于查询。数据集用于从 SP 查询中检索结果。 现在我们正在研究使用 Entity Framework 针对同一个数据库开发另一个项目。在查询数据
我有一个每 10 秒运行一次的 Windows 服务......每次运行时,它都会获取一些测试数据,对其进行修改并使用 EntityFramework 将其保存到数据库中。但是,在每一秒运行时,当我尝
我对在我们的场景中仅将 Entity Framework 与存储过程一起使用的合理性有疑问。 我们计划拥有一个 N 层架构,包括 UI、BusinessLayer (BLL)、DataAccessLa
当我使用 Entity Framework 时,我想在上下文中查询出一条记录并将其添加到具有相同架构的另一个上下文中,在查询出记录后,我将其从上下文中分离出来,但是相关实体都没有了,是吗?有什么办法解
我正在使用 Entity Framework 5 构建 ASP.Net MVC4 Web 应用程序。我必须使用现有的 sql server 数据库,但也想使用 Code First,所以我遵循了本教程
在 Entity Framework 4.0 中使用 T4 模板创建 POCO 会丢失什么?为什么使用 Entity Framework 4.0 时的默认行为不创建 POCO? 最佳答案 你会失去很多
我在网上使用 Repository Pattern 和 EF 看了很多例子。但他们都没有真正谈到与相关实体的合作。 就像说用户可以有多个地址。 IUserRepository User CreateU
我是一名优秀的程序员,十分优秀!