2013-10-26 5 views
-1

1 : 1 관계가 많은 학교 프로젝트가 있습니다 (연락처에는 주소가 많을 수 있음). 그러나 나는 그것을 올바르게 씨 뿌리는 법을 모른다.씨앗 데이터와 ICollection?

내 데이터 모델 연락처에 virtual ICollection<Address> Addresses이 있고 주소 개체의 외래 키가 ContactId입니다.

그래서 여기에 내 시드 데이터가 있습니다 (코드 첫 번째) 그리고 검색 표시 줄에 연락처 성 (last name)을 입력하면 해당 연락처 (주소 정보)의 모든 정보가 표시됩니다.

그래서 어떻게 내 시드 데이터에서 정보를 연결합니까? 그래서 검색 할 때 그 정보를 가져옵니다.

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

은 학교 프로젝트가 있다면, 밖으로 물건을 파악하기 위해 그것의 * 포인트 *가되지 않습니다 :/다른

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

을 전환 할 경우 추가로 필요합니다 그리고 SeedContactsAndAddress 방법은 다음과 같이 보일 것이다 너 자신을 위해서? – RyanR

+0

예. 그리고 나는 여러 가지 일을 시도하고 마지막으로이 포럼에 대한 도움을 요청하기 전에 여러 차례 내 모든 과제를 다시해야만했습니다. – Pope43

답변

1

연락처와 주소를 모두 시드하는 다른 방법이있을 수 있습니다.

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가 유효하지 않은 상태에서 주소를 DB에 삽입 할 수 없습니다. – Mark