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();
}
}
}
은 학교 프로젝트가 있다면, 밖으로 물건을 파악하기 위해 그것의 * 포인트 *가되지 않습니다 :/다른
을 전환 할 경우 추가로 필요합니다 그리고 SeedContactsAndAddress 방법은 다음과 같이 보일 것이다 너 자신을 위해서? – RyanR
예. 그리고 나는 여러 가지 일을 시도하고 마지막으로이 포럼에 대한 도움을 요청하기 전에 여러 차례 내 모든 과제를 다시해야만했습니다. – Pope43