[英]One-to-many without exposing child collections and/or foreign keys in the model
[英]Too Many Foreign Keys in a One-to-Many
此问题类似于:
和
但是还不足够让我寻求解决方案。
我有一个“团队”模型,其中一个外键指向另一个名为“房间”的模型,一个团队只能有一个房间。 房间模型有两个团队“ 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; }
}
再次感谢您的所有帮助。
实体框架行为正常。 如果设置“团队室”属性,则只会影响该表中的RoomID
。 其他列在“房间”表中更改。
您的房间表应该只有一个ICollection<Team>
。 如果您希望一个房间有多个团队列表, TeamTypeID
向“团队”表中添加一个TeamTypeID
。 这样,一个会议室可以拥有与TeamTypes一样多的团队列表。
因为您在“房间”表中有多个ICollection<Team>
,所以正在“团队”表中生成其他列。
基于很多假设,我认为这两个类应该看起来像
球队:
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)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.