2013-10-26 48 views
-1

我有一個一對多的關係(聯繫可以有很多地址)的學校項目。但我不知道如何正確播種。帶ICollection的種子數據?

在我的數據模型中聯繫人有一個virtual ICollection<Address> Addresses,地址對象的外鍵爲ContactId

所以這裏是我的種子數據(代碼優先)我需要這樣做,當我在搜索欄中輸入聯繫人姓氏時,它將提取該聯繫人(地址信息)上的所有信息。

那麼,我如何將我的種子數據中的信息聯繫在一起,因此當您搜索它時,會拉起它應該是什麼?

namespace Success.Data.Migrations 
{ 
    public class Seeder 
    { 
     public static void Seed(SuccessContext context, 
      bool seedContacts = true, 
      bool seedAddresses = true) 

     { 
      if (seedContacts) SeedContacts(context); 
      if (seedAddresses) SeedAddresses(context); 
     } 

     private static void SeedContacts(SuccessContext context) 
     { 
      context.Contacts.AddOrUpdate(l => l.LastName, 
       new Contact() { FullName = "Darth Vader", FirstName = "Darth", LastName = "Vader", }, 
       new Contact() { FullName = "Luke Skywalker", FirstName = "Luke", LastName = "Skywalker", }, 
       new Contact() { FullName = "Tony Stark", FirstName = "Tony", LastName = "Stark", }, 
       new Contact() { FullName = "Ricky Bobby", FirstName = "Ricky", LastName = "Bobby", }, 
       new Contact() { FullName = "Trix Rabbit", FirstName = "Trix", LastName = "Rabbit", }); 

      context.SaveChanges(); 

     } 

     private static void SeedAddresses(SuccessContext context) 
     { 
      context.Addresses.AddOrUpdate(h => h.HomeAddress, 
       new Address() { HomeAddress = "1300 DeathStar", BusinessAddress = "444 Imperial Fleet", PoBox = "PO Box 1335", ContactId = 1, }, 
       new Address() { HomeAddress = "1997 Endor", BusinessAddress = "448 Rebel Fleet", PoBox = "PO Box 1339", ContactId = 2, }, 
       new Address() { HomeAddress = "1224 Malibu Point", BusinessAddress = "657 Stark Industries", PoBox = "PO Box 1337", ContactId = 3, }, 
       new Address() { HomeAddress = "9978 Fast LN.", BusinessAddress = "532 NASCAR Race Track", PoBox = "PO Box 1333", ContactId = 4, }, 
       new Address() { HomeAddress = "9864 Cerial Box LN", BusinessAddress = "8432 Kellog Dr.", PoBox = "PO Box 1338", ContactId = 5, }); 

      context.SaveChanges(); 

     } 
    } 
} 
+0

如果它是一所學校的項目,是不是它的*點*搞清楚這些事情爲自己? – RyanR

+0

是的。我已經嘗試了幾種不同的東西,並且不得不重新做好我的整個任務,然後才終於在此論壇上尋求幫助 – Pope43

回答

1

你可以有另一種方法,種子的聯繫人和地址。您將需要一個額外的if/else開關

if (seedContacts && seedAddresses) 
{ 
    SeedContactsAndAddress(context); 
} 
else 
{ 
    if (seedContacts) SeedContacts(context); 
    if (seedAddresses) SeedAddresses(context); 
} 

而且SeedContactsAndAddress方法是這樣的:

private static void SeedContactsAndAddress(StoreContext context) 
{ 
    // Each Address, which I believe is a collection in this case, but there is only 
    // one, will have to be created and added to each contact. 

    var addressesForDarthVader = new List<Address> 
    { 
     new Address { HomeAddress = "1300 DeathStar", BusinessAddress = "444 Imperial Fleet", PoBox = "PO Box 1335" } 
     // Add more addresses for Darth Vader if you need to 
    }; 

    // Rinse and repeat for the other contacts; 

    context.Contacts.AddOrUpdate(l => l.LastName, 
     new Contact() { FullName = "Darth Vader", FirstName = "Darth", LastName = "Vader", Addresses = addressesForDarthVader }, 
     new Contact() { FullName = "Luke Skywalker", FirstName = "Luke", LastName = "Skywalker", }, 
     new Contact() { FullName = "Tony Stark", FirstName = "Tony", LastName = "Stark", }, 
     new Contact() { FullName = "Ricky Bobby", FirstName = "Ricky", LastName = "Bobby", }, 
     new Contact() { FullName = "Trix Rabbit", FirstName = "Trix", LastName = "Rabbit", }); 

    context.SaveChanges(); 
} 
+0

非常感謝您的讚賞,您所做的事情是有道理的......但我只是爲每個項目添加了一個ContactId我的地址1-5。我相信那也行得通? – Pope43

+0

您仍然需要一個單獨的方法來執行客戶和地址。你將不得不首先調用客戶的Savechanges,並希望生成的contactId的順序是正確的...我猜這些是自動增量Id的?並假設地址與聯繫人有外鍵關係,那麼如果contactId無效,您將無法將地址插入到數據庫中。 – Mark