问题描述
我试图在两个实体之间建立一对零或一个关系,我希望从属实体仍包含其自己的身份列,而不是它是共享密钥。
我想尽可能多地遵循约定,而不是显式声明不需要显式声明的任何内容(因此,没有不必要数据注释或流利的api子句)
实体:
公共类文件
{
public int FileId {get; set;}
//一些省略的文件属性
public virtual Task Task {get; set;}
}
public class Task
{
public int TaskId {get; set;}
//一些省略的任务属性
public int FileId {get; set;}
public virtual File File {get; set;}
}
受保护的覆盖无效OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< File>()。HasOptio nal(f => f.Task).WithRequired(t => t.File);
base.OnModelCreating(modelBuilder);
}
这会创建一个奇怪的关系,其中TaskId既是PK又是FK列。任务表。我认为该值应该与文件ID相同? (这是一个问题:))
那么,如何使TaskId保持其自己的顺序值并使FileId成为Files表的外键? p>
或者在1-0..1的情况下,我宁愿摆脱TaskId属性,而让FileId成为PK / FK属性?
干杯!
双向一对一关系。
因此,要么继续使用您现在拥有的内容- 。只需摆脱 Task $ c $中的
TaskId
或 FileId
属性之一即可c>并将其余的PK设为EF(EF将自动将其用作FK,因为这是默认的EF 一对一
关系模型)。
或者从 Task
摆脱 FieldId
属性,并使用以下流畅的配置(全部都是必要的):
modelBuilder.Entity< File>()
.HasOptional(f => f。任务)
.WithRequired(t => t.File)
.Map(m => m.MapKey( FileId))
.WillCascadeOnDelete();
但是我建议使用第一种方法(如果没有特殊的理由不像现有方法那样做)数据库等),因为它得到了更好的支持-第二个在SQL查询中包含一些 LEFT OUTER JOIN
s,如您在这篇文章中所见。
I am trying to establish a One-to-Zero-or-One relationship between two entities and I want the dependent entity to still contain its own Indentity column, instead of it being a shared key.
I want to do as much as possible following the conventions and not declaring explicitly anything that does not require explicit declaration (so, no unnecessary data annotations or fluent api clauses)
The entites:
public class File
{
public int FileId {get;set;}
//some omitted file properties
public virtual Task Task {get;set;}
}
public class Task
{
public int TaskId {get;set;}
//some omitted task properties
public int FileId {get;set;}
public virtual File File {get;set;}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<File>().HasOptional(f => f.Task).WithRequired(t => t.File);
base.OnModelCreating(modelBuilder);
}
This creates a weird relation where the TaskId is both PK and FK column of the Tasks table. Which, I think means that it should have the same value as the file ID? (That is a question:) )
So, how do I make TaskId hold its own, sequential value and have FileId become a foreign key to the Files table?
Or maybe in case of 1-0..1 I should rather get rid of the TaskId property and make FileId the PK/FK property?
Cheers!
Bidirectional one-to-one
relationship with explicit FK property is not supported.
So either continue using what you have now - Shared Primary Key association. Just get rid of one of the TaskId
or FileId
properties from Task
and make the remaining a PK (EF will automatically use it as FK because that's the default EF one-to-one
relationship model).
Or get rid of the FieldId
property from Task
and use the following fluent configuration (all is necessary):
modelBuilder.Entity<File>()
.HasOptional(f => f.Task)
.WithRequired(t => t.File)
.Map(m => m.MapKey("FileId"))
.WillCascadeOnDelete();
But I would recommend using the first approach (if there is no special reason of not doing it like existing database etc.) because it's better supported - the second includes some LEFT OUTER JOIN
s in SQL queries as you can see from this post EF - WithOptional - Left Outer Join?.
这篇关于EF代码优先-在不共享PK / FK的情况下配置一对零或一对关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!