I'm trying to get my head around how to add new records to a table that have a foreign key to another table using LINQ to Entities. I'll try and explain:
Say I have a simple database with two tables. For example lets call one LibraryItem and the other LibraryItemStatus. The LibaryItem has a foreign key to the LibraryItemStatus table so an item can be assigned a status.
My question is how do I create a new LibraryItem using LINQ To Entities and associate it with a status? My code below must be wrong but I cannot find any examples to help.
LibraryEntities entities = new LibraryEntities();
LibraryItem item = new LibraryItem();
item.Name = "some name";
item.SomeAttribute = "x";
// What do I do here?
// This seems wrong as I don't want a new status.
item.Status = new Status() { id = 1 };
// This seems wrong as I don't really want to have to query the repository
item.Status = entities.Status.First(s => s.StatusID == 1);
//
entities.AddToItems(item);
entities.Savechanges();