实体框架代码第一个一对一的必需-必需关系 [英] Entity Framework Code First One-to-One Required-Required Relationship

查看:107
本文介绍了实体框架代码第一个一对一的必需-必需关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Entity Framework Code First 4.3.1时,可以创建多个一对一的关系。也就是说,关系的每一端都有一个实体。

When using Entity Framework Code First 4.3.1 it is possible to create relationships with a multiplicity of 1-to-1. That is, one entity on each end of the relationship.

可以将一对一关系配置为必需-必需必填(可选) ^。但是,当我在两者之间切换时,看不到任何差异:

It is possible to configure 1-to-1 relationships to be required-required or required-optional ^. However, when I switch between the two I do not see any differences in:


  • 生成的数据库模式。我的目标是SQL Server 2008。

  • EF的运行时行为。

尽管已将关系配置为 required-required ,但我仍可以创建 RequiredPrincipalAs 记录而没有相应的 RequiredDependentAs 记录。这似乎与 HasRequired(...)的文档相矛盾:

As such, I am able to create a RequiredPrincipalAs record without a corresponding RequiredDependentAs record, despite the relationship being configured as required-required. This seems to contradict the documentation for HasRequired(...):


从该实体配置所需的关系类型。除非指定了这种关系,否则实体类型的实例将无法保存到数据库。数据库中的外键将不可为空。

Configures a required relationship from this entity type. Instances of the entity type will not be able to be saved to the database unless this relationship is specified. The foreign key in the database will be non-nullable.

http://msdn.microsoft.com/zh-cn/library/gg671317

必填必需的关系实体:

public class RequiredPrincipalA
{
    public int Id { get; set; }
    public virtual RequiredDependentA DependentA { get; set; }
}

public class RequiredDependentA
{
    public int Id { get; set; }
    public virtual RequiredPrincipalA PrincipalA { get; set; }
}

必填项关系实体:

public class RequiredPrincipalB
{
    public int Id { get; set; }
    public virtual OptionalDependentB DependentB { get; set; }
}

public class OptionalDependentB
{
    public int Id { get; set; }
    public virtual RequiredPrincipalB PrincipalB { get; set; }
}

DbContext和模型配置:

The DbContext and model configuration:

public class AppContext : DbContext
{
    public DbSet<RequiredPrincipalA> PrincipalAs { get; set; }
    public DbSet<RequiredDependentA> DependentAs { get; set; }

    public DbSet<RequiredPrincipalB> PrincipalBs { get; set; }
    public DbSet<OptionalDependentB> DependentBs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<RequiredPrincipalA>()
            .HasRequired(o => o.DependentA)
            .WithRequiredPrincipal(o => o.PrincipalA);

        modelBuilder.Entity<RequiredPrincipalB>()
            .HasOptional(o => o.DependentB)
            .WithRequired(o => o.PrincipalB);
    }
}

测试代码:

Database.SetInitializer(new DropCreateDatabaseAlways<AppContext>());

using (var ctx = new AppContext())
{
    ctx.Database.Initialize(force: false);

    ctx.PrincipalAs.Add(new RequiredPrincipalA());
    ctx.PrincipalBs.Add(new RequiredPrincipalB());

    ctx.SaveChanges();
}

我知道我可以添加 [必需] 数据属性添加到 RequiredPrincipalA.DependentA RequiredDependentA.PrincipalA 的导航属性。这将导致EF验证,以防止出现上述情况。但是,我不想这样做,因为它还可以在更新现有实体时验证导航属性是否已填充。这意味着应用程序必须为每次更新在关系的另一端预取实体。

I am aware I could add a [Required] data attribute to the navigation properties of RequiredPrincipalA.DependentA and RequiredDependentA.PrincipalA. This would cause EF validation to prevent the scenario above. However, I do not want to do this because it also validates the navigation property is populated when updating an existing entity. This means the application has to pre-fetch the entity at the other end of the relationship for every update.

为什么我看不到EF的行为有什么不同更改必需-必需必需-可选之间的关系时?

Why do I not see any difference in the behaviour of EF just when changing a relationship between required-required and required-optional?

^ 请注意,可选-还支持optional,但这不构成我的问题的一部分。配置可选-可选关系后,生成的数据库架构和运行时行为存在明显差异。

推荐答案

我不知道为什么在这种情况下允许使用required-required,但是由于关系是基于主键构建的,因此它不能存在于数据库中。必需-意味着如果不存在相关的B,则不能插入A;如果不存在相关的A,则不能插入B =>不能插入A或B。

I don't know why required-required is allowed for this case but it cannot exist in the database because relation is build on primary keys. Required-required means that A cannot be inserted if related B doesn't exist and B cannot be inserted if related A doesn't exist => neither A or B can be inserted.

数据库关系始终具有主体和从属实体-主体可以始终存在而没有从属实体。

Database relation has always principal and dependent entity - principal can always exist without dependent.

仅当A和B都为EF时,才可以实现EF中的实际需求映射到同一表(表拆分),因为在这种情况下,它们都可以通过单个插入命令插入。

Real required-required in EF can be achieved only when both A and B are mapped to the same table (table splitting) because in such case they are both inserted with single insert command.

这篇关于实体框架代码第一个一对一的必需-必需关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆