映射HasOptional()外键。WithOptionalDependent()的实体框架6的关系 [英] Mapping foreign key in HasOptional().WithOptionalDependent() relation in Entity Framework 6

查看:2399
本文介绍了映射HasOptional()外键。WithOptionalDependent()的实体框架6的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实体框架6.1.3以下数据模型:

I have the following data-model in Entity Framework 6.1.3:

using System.Data.Entity;

public class Student
{
    public int Id { get; set; }
    public virtual Contact Contact { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public virtual Student Student { get; set; }
}

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder builder)
    {
        builder.Entity<Contact>()
            .HasOptional(x => x.Student)
            .WithOptionalDependent(x => x.Contact)
            .WillCascadeOnDelete(true);
    }
}

public static class Program
{
    private static void Main()
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

        using (var context = new MyContext())
            context.Database.Initialize(force: true);
    }
}

当我推出这个代码,我得到完全正确表结构我的目标为:

When I launch this code, I get exactly the right table structure I am aiming for:

dbo.Contacts
    Id (PK)
    Student_Id (FK, NULL, CASCADE ON DELETE)

dbo.Students
    Id (PK)

不过,现在我想添加 student_id数据属性是在联系实体使用。因此,我可以读 student_id数据而不需要通过加入其他表 .Student.Id 导航。

However, now I would like to add the Student_Id property to be available in the Contact entity. So I can read the Student_Id without needing to join the other table through .Student.Id navigation.

如果我添加属性到联系的实体,我最终要么有两列 student_id数据 Student_Id1 ,或者我结束了一个错误信息说在类型每个属性名称必须是唯一的。

If I add the property to the Contact entity, I end up either with two columns Student_Id and Student_Id1, or I end up with an error message saying Each property name in a type must be unique..

列已在数据库中,所有我需要的是有它的实体为好,为什么这么多麻烦?有没有解决的办法?

The column is already in the database, all I need is to have it in the entity as well, why is it so much trouble? Is there a solution?

推荐答案

如果你想声明的依赖实体的FK财产的一对一的关系,我怕你必须把它作为一个PK了。 EF代码优先要求依赖实体的PK一定的关系FK太:

If you want to declare the FK property in the dependent entity in an one to one relationship, I'm afraid you must use it as a PK too. EF Code First requires that PK of the dependent entity must be FK of the relationship too:

public class Contact
{
    [Key,ForeignKey("Student")]
    public int StudentId { get; set; }
    public virtual Student Student { get; set; }
}



但我认为这不是你在找什么。所以,我觉得你有三个选择在这里:

But I think this is not what you are looking for. So, I think you have three options here:


  • 您保留当前的关系配置

  • 创建一个真正的一对一的关系

  • 创建一个一对多的关系

  • You preserve your current relationship configuration.
  • Create an authentic one to one relationship.
  • Create an one to many relationship

这是我的经验,最后一个是最调节什么是你想实现(但是这是我的看法)。在这种情况下,您可以用FK财产的工作,只要你想,唯一的就是你需要更改学生联系导航属性$ C>通过集合(或省略该导航属性,并创建一个单向关系。):

By my experience the last one is the most adjusted to what are you trying to achieve (but that is my opinion). In this case you can work with the Fk property as you want, the only is you need to change the Contact navigation property on Student by a collection (or omit this nav. property and create an unidirectional relationship):

public class Student
{
    public int Id { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
}



该配置是这样的:

The configuration would be this way:

 builder.Entity<Contact>()
        .HasOptional(x => x.Student)
        .WithMany(x => x.Contacts)
        .HasForeignKey(x => x.StudentId)
        .WillCascadeOnDelete(true);



更新



第四个选择是创建两个单向关系:

Update

A fourth option could be create two unidirectional relationships:

 builder.Entity<Contact>()
        .HasOptional(x => x.Student)
        .WithMany()
        .HasForeignKey(x => x.StudentId)
        .WillCascadeOnDelete(true);

 builder.Entity<Student>()
        .HasOptional(x => x.Contact)
        .WithMany()
        .HasForeignKey(x => x.ContactId)
        .WillCascadeOnDelete(true);



但这一选择打破了两个表之间的关系实

But this option breaks the real relation between the two tables.

这篇关于映射HasOptional()外键。WithOptionalDependent()的实体框架6的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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