Master Details: How do I add a detail record?
This should be simple: If I have an entity framework master detail relationship how do I add a detail record in code? Let's say I have parent table/entity called Book and a child/lookup table/entity called Chapter containing a ChapterTitle field/property. I want the user to be able to type ChapterTitles into a TextBox then click an "Add Chapter Title" button. The new ChapterTitle will then appear in a ListView that is bound to the Chapters table/entity via a CollectionViewSource.
I'm NOT using MVVM (and I'm not seeking an answer involving MVVM). I'm using drag and drop from the data sources window as described in this Julie Lerman post. The difference in my case is that instead of displaying and adding details via a datagrid I'm using a ListView and a textbox for the user to add the new Chapter Title.
Drag and drop has created two CollectionViewSource entries in the XAML:
<Window.Resources>
<CollectionViewSource x:Key="booksViewSource" d:DesignSource="{d:DesignInstance my:Book, CreateList=True}" />
<CollectionViewSource x:Key="bookChaptersViewSource" Source="{Binding Path=ChaptersNav, Source={StaticResource booksViewSource}}" />
<开发者_Python百科/Window.Resources>
The auto-generated code looks like this:
private System.Data.Objects.ObjectQuery<Book> GetBooksQuery(BookEntities bookEntities)
{
// Auto generated code
System.Data.Objects.ObjectQuery<BookNamespace.Book> booksQuery = bookEntities.Books;
// Update the query to include Chapters data in Books. You can modify this code as needed.
booksQuery = booksQuery.Include("Chapters");
// Returns an ObjectQuery.
return booksQuery;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BookEntities bookEntities = BookEntities();
// Load data into Books. You can modify this code as needed.
System.Windows.Data.CollectionViewSource booksViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("booksViewSource")));
System.Data.Objects.ObjectQuery<BookNamespace.Book> booksQuery = this.GetBooksQuery(bookEntities);
booksViewSource.Source = booksQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
}
What code do I need to add to my button_click event to take the user's chapter title from the text box and add it as a new detail record via the detail's CollectionViewSource (bookChaptersViewSource)? I'd expect this to be quite simple, but I've spent way too much time trying and not having any luck. Thanks in advance!
Sorry for VB, but it should be easy enough to translate:
Dim c as Chapter = New Chapter
c.Title = ChapterTitleTextBox.Text
context.AddObject(c, "Chapters")
MyBook.Chapters.Add(c)
That is really all there is to it. Create the object, add it to the context, add it to the detail collection for the Master. In the example above, in the Context.AddObject method, "Chapters" refers to the name of the EntitySet of the class you are adding to the context.
The CollectionViewSource, having its source set to the Books Chapter collection, should pick up the addition of the chapter and the UI should be updated with a new element. Generally you don't have to interact with the CollectionViewSource except when changing the View (filtering, sorting, etc).
I am probably not understanding the problem completely, but usually you need to create a new child object and assign it to parent's list of children. When you save the parent, it should save the new child object. Or you can assign the parentid of the new child object and save it.
精彩评论