Note that there are some explanatory texts on larger screens.

plurals
  1. POSeed Data with ICollection?
    primarykey
    data
    text
    <p>I have a school project with a one to many relationship (Contact can have many Addresses). But I don't know how to seed it correctly.</p> <p>In my Data models Contact has a <code>virtual ICollection&lt;Address&gt; Addresses</code> and the address object has the foreign key of <code>ContactId</code>.</p> <p>So here is my seed data (code first) And i need to make it so when i type in the contacts last name in a search bar it will pull up all the info on that contact (address Info). </p> <p>So how do i associate the info together in my seed data so when you search it pulls up what it is supposed to?</p> <pre><code> 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 =&gt; 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 =&gt; 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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload