一对多

时间:2015-10-06 20:58:11

标签: c# entity-framework ef-code-first

此问题类似于:

Code First Multiple Foreign Keys for One to Many Relation

Entity Framework Code First - two Foreign Keys from same table

但是我没有足够相似的解决方案。

我有一个模型“团队”,其中一个外键具有另一个名为“Room”的模型,一个团队只能有一个房间。房间模型有两个团队列表“p2teams”和“p3teams”。在teams表中,它实际上为房间创建了三个不同的id列:“RoomID”,“Room_RoomID”和“Room_RoomID1”。

这导致我尝试设置团队的房间(例如exampleTeam.Room = exampleRoom),它只影响RoomID列,而不影响其余部分。此外,我真的没有完全了解得到这个结果的事情。

以下是我的模型,数据库图表以及团队表格中的一些示例数据。谢谢你的帮助!

团队模型:

public class Team
{
    public int TeamID { get; set; }

    public int HighschoolID { get; set; }
    public virtual Highschool Highschool { get; set; }
    public int ContestID { get; set; }
    public virtual Contest Contest { get; set; }

    [ForeignKey("Room")]
    public int? RoomID { get; set; }
    public virtual Room Room { get; set; }

    public string Warning { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

房型:

public class Room
{
    public int RoomID { get; set; }

    public virtual ICollection<RoomUsage> RoomUsages { get; set; }

    public string Name { get; set; }

    public int? AvailibleSeats { get; set; }
    public int? TotalSeats { get; set; }

    public virtual ICollection<Student> p1Students { get; set; }
    public virtual ICollection<Team> p2Teams { get; set; }
    public virtual ICollection<Team> p3Teams { get; set; }
}

Relevant DB Diagram Image

First Few Rows of Teams Table

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

实体框架表现正常。如果您设置Teams Room属性,则仅影响该表中的RoomID。其他列在房间表中更改。

您的房间表应该只有一个ICollection<Team>。如果您希望Room有多个团队列表,请在Teams表中添加TeamTypeID。然后,一个房间可以拥有与TeamTypes一样多的团队列表。

由于Rooms表中有多个ICollection<Team>,因此它会在Teams表中生成其他列。

基于很多假设,我认为这两个类应该是

小组:

public class Team
{

    public int TeamID { get; set; }

    public int? RoomID { get; set; }

    public string Warning { get; set; }

    public virtual Room Room { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public virtual ICollection<Contest> Contests { get; set; }
}

间:

public class Room
{
    public int RoomID { get; set; }

    public string Name { get; set; }

    public int? AvailableSeats { get; set; }

    public int? TotalSeats { get; set; }

    public virtual ICollection<Student> Students { get; set; }

    public virtual ICollection<Team> Teams { get; set; }

    public virtual ICollection<Usage> Usages { get; set; }
}

我已经创建了一个创建数据库表的脚本,相信会在更多假设的基础上实现您的目标:

• A student has
    One: Team, HighSchool, Room 
• A team has 
    One: Room
    Many: Contests 
• A room has 
    Many: Teams, Students, Usages 
• A contest has 
    Many: Teams 

如果您仍想首先使用代码,可以使用Entity Framework从这些表中生成Code First类

create table dbo.HighSchools
(
    HighSchoolID int primary key,
    Name varchar(50)

)

create table dbo.Rooms
(
    RoomID int primary key,
    Name varchar(50),
    AvailableSeats int,
    TotalSeats int
)

create table dbo.Usages
(
    UsageID int primary key,
    Name varchar(50)
)

--If a Usage is Unique to each room I would remove this table and add RoomID to the Usages Table
create table dbo.RoomUsages
(
    UsageID int foreign key (UsageID) references Usages(UsageID),
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    primary key (UsageID, RoomID)
)


create table dbo.TeamTypes
(
    TeamTypeID int primary key,
    Name varchar(50)
)

create table dbo.Teams
(
    TeamID int primary key,
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    TeamTypeID int foreign key (TeamTypeID) references TeamTypes(TeamTypeID),
    Warning varchar(50)
)

--If a Student's room will always be the same as their Team's room I would remove the RoomID column from either Teams or Students 
create table dbo.Students
(
    StudentID int primary key,
    HighSchoolID int foreign key (HighSchoolID) references HighSchools(HighSchoolID),
    TeamID int foreign key (TeamID) references Teams(TeamID),
    RoomID int foreign key (RoomID) references Rooms(RoomID),
    Name varchar(50),
    DOB date,

)

create table dbo.Contests
(
    ContestID int primary key,
    Name varchar(50)
)

create table dbo.ContestTeams
(
    ContestID int foreign key (ContestID) references Contests(ContestID),
    TeamID int foreign key (TeamID) references Teams(TeamID),
    primary key (ContestID, TeamID)
)

DB Diagram