如何在具有不同PK和FK的流利api中配置一对零和一对一关系 [英] How to configure one to zero and one to one relationship in fluent api with different PK and FK

查看:102
本文介绍了如何在具有不同PK和FK的流利api中配置一对零和一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,可以通过教程学习流利的API.我阅读了一些关于 1-0,1-* 的SO解决方案,但是我无法正确理解它们.我只是想使用 fluent API 来设置一对一和一对零关系,前提是不遵循约定.

I am a beginner and learning fluent API from tutorials. I read the some of the already given SO solution about 1-0, 1-* but I could not understand them properly. I simply want to use fluent API to set up One to One and One to Zero Relationship provided no convention followed.

房屋和房间(可怕的例子)

House and Room (terrible example)

要求:

1. One House can have zero room or ONE room MAX.
2. One Room must be inside a house.
3. If a room is deleted House SHOULD NOT Get Deleted
4. If a House is getting deleted and there is a room then
  User should first delete the room then only House should be allowed to be deleted.


public class House
{
    public int HouseId { get; set; }
    public string HouseName { get; set; }
    public virtual Room Room { get; set; }
}

public class Room
{
    public int RoomId { get; set; }
    public string RoomName { get; set; }
    public int HouseId { get; set; }
    public virtual House House { get; set; }
}

因此,没有房屋的房间不能存在,但是没有房屋的房子可以存在.另外,如果一间房子有房间,则只能有一个房间.

So, Room cannot exist w/o a House but a House can exist w/o a room. Also, in case if a House has room, it can only have one.

         dBModelBuilder.Entity<House>()
            .HasOptional(f => f.Room)
            .WithRequired(s => s.House);

我看到了一些解决方案,但是他们告诉我们将 PK和FK设置为​​相同.但是我不想.有没有一种方法可以实现我想要的不设置PK和FK相同.我不希望HouseID成为我的房间班级的PK.在我的情况下,校长是房屋,受抚养人是房间.我是否需要添加诸如"Optional Dependent"或"Optional Principal" 之类的东西.有人可以指导我,我是初学者.

I saw some solution but they are telling to set the PK and FK same. But I don't want to do that. Is there a way to achieve what I want w/o setting PK and FK same. I DO NOT want the HouseID to be PK of my Room class. Also in my case principal is House and Dependent is Room. Do I need to add soemthing like "Optional Dependent" or "Optional Principal". Can some one please guide me I am a beginner.

此外,我是否需要从任何模型中删除导航属性?那是无关紧要的吗?

Also, Do I need go remove navigation property from any of my MODELS? Is that extraneous?

如何告诉EF将Room类的HouseId用作FK.

How to tell EF to use HouseId of Room class as FK.

推荐答案

在我的案例中,校长是房屋",受抚养人是房间"

Also in my case principal is House and Dependent is Room

那么您在正确的轨道上

modelBuilder.Entity<House>()
    .HasOptional(f => f.Room)
    .WithRequired(s => s.House)

因为在EF一对一关系中,要求方始终是主体.仅当双方都是必需的或双方都是可选的时,才需要指定主体/从属.

because in EF one-to-one relationship the required side is always the principal. You need to specify the principal/dependent only when both sides are required or both sides are optional.

问题是您需要在从属实体中使用与PK不同的FK,而PK是此类关系的默认EF模型(即

The problem is your requirement of using different FK in the dependent entity than the PK, which is the default EF model for such relationship (so called Shared Primary Key Association), and is supported well. From the other side, one-to-one FK association is supported, but with some limitations. More specifically, explcit FK property like HouseId in your Room is not supported - there is no HasForeignKey fluent method for this type of configuration, and it's by purpose. I can't say why, but it's a fact. If you try playing with [ForeignKey] attribute, you'll get quite unexpected results, so don't do that.

删除该属性:

public class Room
{
    public int RoomId { get; set; }
    public string RoomName { get; set; }
    public virtual House House { get; set; }
}

,并结合使用 Map 流利的API和 MapKey 来指定FK列名称:

and use Map fluent API with MapKey to specify the FK column name:

modelBuilder.Entity<House>()
    .HasOptional(f => f.Room)
    .WithRequired(s => s.House)
    .Map(c => c.MapKey("HouseId"));

这将为您提供所需的数据库表设计(尽管没有办法将 HouseId 列约束为唯一,除非手动编辑迁移).

This will give you the desired database table design (although there is no way to constrain HouseId column to be unique, except manually editing the migration).

这篇关于如何在具有不同PK和FK的流利api中配置一对零和一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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