简体   繁体   English

实体框架0..1到0关系

[英]Entity Framework 0..1 to 0 relation

class First
{
    [Key]
    public int Id { get; set; }
}

class Second
{
    [Key]
    public int Id { get; set; }

    public int? First_Id { get; set; }

    [ForeignKey("First_Id")]
    public First First { get; set; }
}

public class SecondMapping : EntityTypeConfiguration<Second>
{
    public SecondMapping () 
        : base()
    {
        this.HasOptional(s => s.First)
            .With ... ???
    }
}

Second may have a reference to First. 第二个可能参考First。 But First never has a reference to Second. 但是First从来没有提到Second。 Is it possible to apply this mapping with Entity Framework 4.1? 是否可以将此映射应用于Entity Framework 4.1?

EDIT: Previously, that was my solution: 编辑:以前,这是我的解决方案:

this.HasOptional(s => s.First)
    .WithOptionalDependent()
    .WillCascadeOnDelete(false);

Second could contain one instance of First (dependent on some kind of Usage-Attribute). 第二个可以包含First的一个实例(取决于某种Usage-Attribute)。 First doesn't contain any instance of Second. 首先不包含Second的任何实例。

One-to-one relation is possible only if foreign key is also primary key of dependent entity. 只有当外键也是从属实体的主键时,才能实现一对一的关系。 So the correct mapping is: 所以正确的映射是:

class First
{
    [Key]
    public int Id { get; set; }
}

class Second
{
    [Key, ForeignKey("First")]
    public int Id { get; set; }
    public First First { get; set; }
}

The reason is that to enforce one-to-one relation in the database foreign key must be unique in the Second entity. 原因是要在数据库中强制执行一对一关系,外键在Second实体中必须是唯一的。 But entity framework doesn't support unique keys - the only unique value for EF is primary key. 但实体框架不支持唯一键 - EF唯一的唯一值是主键。 This is limitation of EF. 这是EF的限制。

There is workaround described on Morteza Manavi's blog . Morteza Manavi的博客上描述了解决方法。 Workaround is based on mapping association as one-to-many and enforcing uniqueness in database by introducing unique constraints in custom database initializer. 解决方法基于将关联映射为一对多,并通过在自定义数据库初始化程序中引入唯一约束来强制执行数据库中的唯一性。

If you're trying to achieve a 1-to-1 relationship, where there is at the most only one Second entity associated to a First entity, and where there is no reverse property try the following: 如果你想实现1对1的关系,那里有最多只有一个Second关联到实体First实体,并且在没有反向财产尝试以下方法:

class First
{
    [Key]
    public Guid FirstId { get; set; }
}

class Second
{
    [Key]
    public Guid FirstId { get; set; }
    public First First { get; set; }
}

class SecondMapping : EntityTypeConfiguration<Second>
{
    public SecondMapping()
    {
        this.HasRequired(s => s.First);
    }
}

You can however use a separate First_id column to do this kind of association, but then you would be effectively creating a 1-to-N relationship. 但是,您可以使用单独的First_id列来执行此类关联,但是您可以有效地创建1对N关系。 It can be 'forced' to be 1-to-1 via a UNIQUE constraint, but you won't be able to create a reverse property due to a limitation in EF (as Ladislav mentioned): 它可以通过UNIQUE约束“强制”为1对1,但由于EF的限制,你将无法创建反向属性(如Ladislav所述):

class SecondMapping : EntityTypeConfiguration<Second>
{
    public SecondMapping()
    {
        this.HasRequired(s => s.First).WithMany().HasForeignKey("First_id");
    }
}

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

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