![](/img/trans.png)
[英]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.