这两个一到一和一到许多实体框架5码首先关系 [英] Both One-To-One and One-To-Many relationships in Entity Framework 5 Code First

查看:131
本文介绍了这两个一到一和一到许多实体框架5码首先关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了整整一天得到这个工作。我学到了很多关于EF的流利API(如的是一篇优秀的文章),但我没有成功。

i tried the whole day to get this working. I learned a lot about EF's Fluent API (e.g. this is an excellent article), however i had no success.

我有三个实体:

public class Address
{
   [Key]
   public virtual int AddressId { get; set; }
   public virtual string AddressString { get; set; }
}
public class User
{
   [Key]
   public virtual int UserId { get; set; }
   public virtual ICollection<Address> Addresses { get; set; }
}
public class House
{
   [Key]
   public virtual int HouseId { get; set; }
   public virtual Address Address { get; set; }
}

和尝试的hasMany,HasOptional的所有组合, WithOptional,WithOptionalDependent WithOptionalPrincipial 我可以为用户认为

protected override void OnModelCreating(DbModelBuilder modelBuilder)

我只是无法得到它的工作。我觉得应该是清楚的,我想要什么。用户可以有多个地址(我想迫使至少一个第一名,但现在我会很高兴,如果一个用户可能有地址选......),而一套房子有且只有一个地址 - 这是需要。如果一所房子的地址将被级联删除,这将是很好。

I just cannot get it to work. I think it should be clear, what i want. A User may have more than one address (in the first place i want to force at least one, but now i would be happy if a user may have the addresses optional...) while a House has exactly one Address - and this is required. It would be nice if the address of a house would be cascading deleted.

推荐答案

我认为以下应为你工作

public class Address
{
    public int AddressId { get; set; }
    public string AddressString { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
}

public class House
{
    public int HouseId { get; set; }
    public virtual Address Address { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<House> Houses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(u => u.Addresses).WithMany();
        modelBuilder.Entity<House>().HasRequired(h => h.Address).WithOptional().Map(m => m.MapKey("AddressId"));
    }
}

请注意,它通常最好指定外键的字段你自己,它可以使生活变得更加简单为你以后。如果你这样做,那么你可以选择重写府如下:

Note that it's often better to specify the foreign key fields yourself, which can make life a lot easier for you later on. If you do this, then you can choose to rewrite House as the following:

public class House
{
    public int HouseId { get; set; }
    public int AddressId { get; set; }
    public virtual Address Address { get; set; }
}



公约将连接AddressId和地址。如果你有房子和地址之间有一个一对一映射,你也可以将它们链接在他们的主键:

Convention will link up AddressId and Address. If you have a one-to-one mapping between House and Address, you could also link them on their primary keys:

public class House
{
    [ForeignKey("Address")]              
    public int HouseId { get; set; }
    public virtual Address Address { get; set; }
}

您提到,您想强制至少有一个地址 - 这个ISN 'T可能的一个一对多的关系。你只能这样做,如果用户有只有一个地址,在这一点上,你可以在用户类中添加必需的AddressId属性。

You mentioned that you would like to enforce at least one address - this isn't possible with a one-to-many relationship. You could only do this if a user had exactly one address, at which point you could add a required AddressId property on the User class.

另外一个评论 - 你做的一切虚拟在你的代码。你只需要进行导航属性虚拟的。

One other comment - you made everything virtual in your code. You only need to make navigation properties virtual.

这篇关于这两个一到一和一到许多实体框架5码首先关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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