简体   繁体   English

EF代码优先-在不共享PK / FK的情况下配置一对零或一对关系

[英]EF code first - configure One-to-Zero-or-One relationship without shared PK/FK

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. 我试图在两个实体之间建立一对零或一个关系,并且我希望从属实体仍包含其自己的Indentity列,而不是作为共享密钥。

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) 我想尽可能多地遵循约定,而不要显式声明不需要显式声明的任何内容(因此,没有不必要的数据注释或流畅的api子句)

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. 这将创建一个奇怪的关系,其中TaskId既是Tasks表的PK列,也是FK列。 Which, I think means that it should have the same value as the file ID? 我认为该值应该与文件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? 因此,如何使TaskId保持其自己的顺序值并使FileId成为Files表的外键?

Or maybe in case of 1-0..1 I should rather get rid of the TaskId property and make FileId the PK/FK property? 也许在1-0..1的情况下,我宁愿摆脱TaskId属性,而将FileId设为PK / FK属性?

Cheers! 干杯!

Bidirectional one-to-one relationship with explicit FK property is not supported. 不支持具有显式FK属性的双向one-to-one关系。

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). 只需从Task TaskIdFileId属性之一,然后将其余的作为PK(EF将自动将其用作FK,因为这是默认的EF one-to-one关系模型)。

Or get rid of the FieldId property from Task and use the following fluent configuration (all is necessary): 或从Task删除FieldId属性,并使用以下流利的配置(所有步骤都是必需的):

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? 但是我建议使用第一种方法(如果没有特殊的理由不像现有数据库那样使用它),因为它得到了更好的支持-第二种方法包括在SQL查询中的一些LEFT OUTER JOIN ,您可以从EF这篇文章中看到- WithOptional-左外部联接? .

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

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