1
我似乎被困在一個特定的問題上。我正在使用實體框架,並且自動生成了我的數據庫中的所有實體。我已經注意到,我有任何表格作爲防止多對多關係的橋接表,實際上並沒有實體;例如,我試圖使用的名爲'DiplomaCertificate'的表通過名爲'DegreeRelationship'的橋接表與名爲'Degree'的錶鏈接。 DegreeRelationship接受這兩個表的主鍵,並將它們用作其主鍵和外鍵。我注意到一個實體我在上下文類得到這個代替:如何使用.NET Entity Framework將數據插入到橋接表中?
modelBuilder.Entity<Degree>()
.HasMany(e => e.DiplomaCertificates)
.WithMany(e => e.Degrees)
.Map(m => m.ToTable("DegreeRelationship").MapLeftKey("DegreeID").MapRightKey("ProgramID"));
在背後說我的代碼,我迄今對應處理插入如下:
public void InsertDiplomaProgram(DiplomaCertificate diplomaProgram, List<EntranceRequirementList> entReqList, List<int> degreeIDs)
{
using (Pathway_Model context = new Pathway_Model())
{
DiplomaCertificate added = null;
EntranceRequirement addedEntReq = null;
added = context.DiplomaCertificates.Add(diplomaProgram);
// create entrance requirement entry for each row entered
foreach (EntranceRequirementList entry in entReqList)
{
EntranceRequirement entReq = new EntranceRequirement()
{
ProgramID = added.ProgramID,
CourseID = entry.CourseID,
Marks = entry.Mark
};
addedEntReq = context.EntranceRequirements.Add(entReq);
}
//create degree diploma entry for each row entered
foreach (int entry in degreeIDs)
{
// normally I would try and use a foreach to populate new entries in the database,
// but right now I am not sure???
}
// commits the add to the databas
context.SaveChanges();
}
}
基本上我試圖把畢業ID,並通過學位證書的名單列入DegreeRelationship表,但我卡在這裏。如果有人可以提供一個建議,我將不勝感激。謝謝!!
我來到這裏的錯誤說,我試圖插入diplomacertificate表,當它應該被插入到degreerelationship – user3042572 2015-03-09 17:58:40
我僅僅指剛不得不改變周圍的東西 - 我不得不把selectedDegree.DiplomaCertificates.Add (diplomaProgram);因爲鍵映射不同。 – user3042572 2015-03-09 18:07:07