עזרה עם EF code first - Seed

odansky

New member
עזרה עם EF code first - Seed

שלום לכולם,
פעם ראשונה שאני עובד עם EF Code First. חלק מההכנה של הdatabase אני משתמש בSEED.
לצורך העניין יש לי שני טבלאות :
Insurer
InsurerName

public class Insurer{
public int InsurerId { get; set; }
public string PrimaryName { get; set; } public string AbiId { get; set; } public virtual Insurer Insurers { get; set; } }public class InsturerName{ public string Name { get; set; } public int InsurerId { get; set; } public virtual Insurer Insurer { get; set; }}
אינדקסים:
Insurer.InsurerId - PK - מספר אוטומטי
Insurer.AbiId - Unique Index

InsuererName.InsurerId + InsurerName.Name - PK

הפונקציה של ה SEED:
var objParent = new Data.Insurer[]{
new Data.Insurer() { AbiId = "001", PrimaryName = "Bla" }
};
var objChild = new Data.InsurerNames[]{ new Data.InsturerName() { Insurer = objParent[0], Name = "Bla" }, new Data.InsturerName() { Insurer = objParent[0], Name = "Bla1" }, new Data.InsturerName() { Insurer = objParent[0], Name = "Bla2" }, new Data.InsturerName() { Insurer = objParent[1], Name = "Cla" },};context.Insurers.AddOrUpdate(one => one.AbiId, objParent );context.InsurerNames.AddOrUpdate(one => new {one.InsurerId, one.Name}, objChild ); context.SaveChanges();

כאשר אני בונה את הDB בפעם הראשונה - הכל נבנה כמו שצריך ללא בעיות או הודעות שגיאה.
כאשר אני מריץ את הקוד על DB קיים - אני מקבל שגיאה של INDEX קיים בטבלה הראשונה (Insurer.AbiId). - אבל בקוד של הטבלה השניה

איך אני יכול לגרום לקוד של ה CHILD - לעשות גם בדיקה על האובייקט הוירטואלי? (כלומר Insurer בתוך ה InsurerName).

תודה מראש,
עמרUK
 
נסה כך

protected override void Seed(EFSeed2.Model1 context){
var objParent = new Data.Insurer[]
{ new Data.Insurer() { AbiId = "003", PrimaryName = "Bla" }};context.Insurers.AddOrUpdate(one => one.AbiId, objParent );int parentId = objParent[0].InsurerId;var objChild = new Data.InsturerName[]{ new Data.InsturerName() { InsurerId = parentId, Name = "Bla1" }, new Data.InsturerName() { InsurerId = parentId, Name = "Bla2" }, new Data.InsturerName() { InsurerId = parentId, Name = "Cla" },};context.InsurerNames.AddOrUpdate(one => new {one.InsurerId, one.Name }, objChild );}
 

nocgod

New member
אופציה מעניינת תהיה לעשות טעינה מקובץ

זה יכול להיות נוח מאוד בשביל בדיקות יחידה ספציפיות. פונקציה אחת שיודעת לטעון מידע מקובץ json.
וגם משומשת ל seed של ה DB
 

odansky

New member
כך פתרתי את הבעיה...

כדי לפתור את הבעיה - הייתי צריך למלא את טבלה הראשונה - ואז לקרוא אותה חזרה לתוך אובייקט.
כאשר קיבלתי את האובייקט חזרה מהDB - האובייקט מכיל גם את האינדקסים שנוצרו. כל מה שנשאר לי לעשות - זה להכניס את האינדקס הרצוי

החלק החשוב הוא:
context.SaveChanges();
var parents = context.Insurers.DefaultIfEmpty().ToList();


הקוד:

var objParent = new Data.Insurer[]{
new Data.Insurer() { AbiId = "001", PrimaryName = "Bla" }
};context.SaveChanges();var parents = context.Insurers.DefaultIfEmpty().ToList();var childObj = new Data.InsturerName[] { new Data.InsturerName {Name = "Bla", InsurerId = GetParentId(insurers, "001") }, new Data.InsturerName {Name = "Bla1", InsurerId = GetParentId(insurers, "001")}, new Data.InsturerName {Name = "Bla2", InsurerId = GetParentId(insurers, "001")}, };context.InsurerNames.AddOrUpdate(one => new { one.InsurerId, one.Name }, insurerNamesObj );context.SaveChanges();private int GetParentId(System.Collections.Generic.List<Data.Insurer> insurers, string abiCode){ return insurers.Where(i => i.AbiId == abiCode).Select(i => i.InsurerId).First();}
 

odansky

New member
יכול להיות שפיספסתי משהו?

הבעיה שאני נתקלתי בה שנסיתי לעשות את כמו מה שאמרת -
הייתי מקבל הודעה של OBJECT NULL REF. מהסיבה שבאוקבייט לא אתחלתי את הID.
&nbsp
&nbsp
 
למעלה