Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF Core-如何创建包含外键列的继承实体类型_C#_Entity Framework - Fatal编程技术网

C# EF Core-如何创建包含外键列的继承实体类型

C# EF Core-如何创建包含外键列的继承实体类型,c#,entity-framework,C#,Entity Framework,我使用逐层次表的方法来实现实体类型中的继承。我定义了3个类: 房间-基本类 子Proom-从文件室继承 概览房间-从房间继承 在DB中,我只有一个名为Room的表,其中包含Subproom和OverviewRoom列。它还包含用于指定其类型的鉴别器列 首先,我尝试将Room类中的所有SubProom列移动到SubProom类中。其中1列包含另一个名为Status的表的外键。完成此操作后,我尝试在OnModelCreating()中为SubProom实体类型指定外键关系。然而,当我尝试这样做时,我

我使用逐层次表的方法来实现实体类型中的继承。我定义了3个类:

房间-基本类

子Proom-从文件室继承

概览房间-从房间继承

在DB中,我只有一个名为Room的表,其中包含Subproom和OverviewRoom列。它还包含用于指定其类型的鉴别器列

首先,我尝试将Room类中的所有SubProom列移动到SubProom类中。其中1列包含另一个名为Status的表的外键。完成此操作后,我尝试在OnModelCreating()中为SubProom实体类型指定外键关系。然而,当我尝试这样做时,我得到了一个编译错误。在EF Core OnModelCreating()方法中,我有以下代码(标记了包含以下错误的行):

modelBuilder.Entity(Entity=>
{
entity.HasOne(d=>d.UnassignedDoctorStatus)
.WithMany(p=>p.Room)**此处发生错误**
.HasForeignKey(d=>d.UnassignedDoctorStatusId)
.OnDelete(DeleteBehavior.Cascade)
.HASSCONSTRAINTNAME(“FK_房间_未分配状态ID”);
});
modelBuilder.Entity()
.HasDiscriminator(“RoomType”)
.HasValue(1)
.HasValue(2);
我得到这个错误:

无法将lambda表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型

我知道我可以通过将另一个类(Status)更改为使用继承类型而不是导航属性的基类型来解决这个问题,但这似乎是错误的做法。我觉得我错过了一些东西。在继承的实体类型中定义外键关系的正确方法是什么

[编辑] 以下是我在此处引用的4个模型的类:

public abstract class Room
{
    public Room()
    {
        InverseLinkedRoom = new HashSet<Room>();
    }

    public int Id { get; set; }
    public int SubMapId { get; set; }
    public string MapLabel { get; set; }
    public string RoomLabel { get; set; }
    public int LeftCoordinate { get; set; }
    public int TopCoordinate { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int? LinkedRoomId { get; set; }
    public int RoomType { get; set; }

    public Room LinkedRoom { get; set; }
    public SubMap SubMap { get; set; }
    public PatientQueue PatientQueue { get; set; }
    public ICollection<Room> InverseLinkedRoom { get; set; }
}

public class SubMapRoom : Room
{
    public int? UnassignedDoctorStatusId { get; set; }

    public Status UnassignedDoctorStatus { get; set; }
}

// Note: Have not yet attempted to move base class members in here
public class OverviewRoom : Room
{
}

public partial class Status
{
    public Status()
    {
        Room = new HashSet<Room>();
    }

    public int Id { get; set; }
    public string EnumId { get; set; }
    public bool Active { get; set; }
    public bool IsFastBlink { get; set; }

    public ICollection<Room> Room { get; set; }
}
公共抽象教室
{
公共房间()
{
InverseLinekDroom=新哈希集();
}
公共int Id{get;set;}
公共整型子PID{get;set;}
公共字符串映射标签{get;set;}
公共字符串RoomLabel{get;set;}
公共int左坐标{get;set;}
公共整数topCoordination{get;set;}
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共int?linkedoomid{get;set;}
public int RoomType{get;set;}
公共房间LinkedRoom{get;set;}
公共子映射子映射{get;set;}
公共PatientQueue PatientQueue{get;set;}
公共ICollection InverseLineKedroom{get;set;}
}
公共类子窗口:房间
{
公共int?未分配的octorstatusId{get;set;}
公共状态未分配的octorstatus{get;set;}
}
//注意:尚未尝试在此处移动基类成员
公共课概述室:会议室
{
}
公共部分类状态
{
公众地位()
{
Room=新HashSet();
}
公共int Id{get;set;}
公共字符串EnumId{get;set;}
公共bool活动{get;set;}
公共布尔值为FastBlink{get;set;}
公共i收集室{get;set;}
}

谢谢大家的帮助。我回顾了我的DB模式,并决定进行一些更改,以消除这个问题。事实证明,我的新模式在代码中使用起来比我原来想象的要容易。事实上,这要容易得多。我想我是想过度设计这个。因此,有时候,解决方案是检查您的模式,并弄清楚它在一开始是否有意义。基本上,我所做的是将继承的类移动到一个具有单独ID的单独表中。因为,归根结底,它们是逻辑上独立的实体类型,并且仅在数据方面相关。在代码中,尽管它们共享一些相同的列,但它们的用途却大不相同


最后,这种方法的唯一缺点是我在另一个表上违反了DRY(其中有5个重复列)。否则,许多其他操作比以前更容易编码。我愿意接受这一点,而不是现在处理这一切。稍后,如果我必须向两个表中添加大量的新列,我可以尝试使用每个层次表。

您需要为所有4个模型添加模型列表。我在末尾添加了4个模型类。好的,那么您的问题是,实际上,您试图从子实体中双向定义关系。我会尝试这样做:
modelBuilder.Entity(Entity=>{Entity.HasOne(d=>d.UnassignedDoctorStatus).HasForeignKey(d=>d.UnassignedDoctorStatusId).OnDelete(DeleteBehavior.Cascade).hassConstraintName(“FK_Room_UnassignedStatusID”)
modelBuilder.Entity(e=>{e.HasMany(i=>i.Room);})尝试了该操作并出现错误:ReferenceNavigationBuilder不包含“HasForeignKey”的定义,并且找不到接受类型为“ReferenceNavigationBuilder”的第一个参数的扩展方法“HasForeignKey”
public abstract class Room
{
    public Room()
    {
        InverseLinkedRoom = new HashSet<Room>();
    }

    public int Id { get; set; }
    public int SubMapId { get; set; }
    public string MapLabel { get; set; }
    public string RoomLabel { get; set; }
    public int LeftCoordinate { get; set; }
    public int TopCoordinate { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int? LinkedRoomId { get; set; }
    public int RoomType { get; set; }

    public Room LinkedRoom { get; set; }
    public SubMap SubMap { get; set; }
    public PatientQueue PatientQueue { get; set; }
    public ICollection<Room> InverseLinkedRoom { get; set; }
}

public class SubMapRoom : Room
{
    public int? UnassignedDoctorStatusId { get; set; }

    public Status UnassignedDoctorStatus { get; set; }
}

// Note: Have not yet attempted to move base class members in here
public class OverviewRoom : Room
{
}

public partial class Status
{
    public Status()
    {
        Room = new HashSet<Room>();
    }

    public int Id { get; set; }
    public string EnumId { get; set; }
    public bool Active { get; set; }
    public bool IsFastBlink { get; set; }

    public ICollection<Room> Room { get; set; }
}