主详细信息:如何添加详细记录?

发布于 2024-11-03 18:55:52 字数 2052 浏览 0 评论 0原文

这应该很简单:如果我有实体框架主详细信息关系,如何在代码中添加详细记录?假设我有一个名为 Book 的父表/实体和一个名为 Chapter 的子/查找表/实体,其中包含 ChapterTitle 字段/属性。我希望用户能够在文本框中输入 ChapterTitles,然后单击“添加章节标题”按钮。然后,新的 ChapterTitle 将出现在通过 CollectionViewSource 绑定到 Chapters 表/实体的 ListView 中。

我没有使用 MVVM(并且我也没有寻求涉及 MVVM 的答案)。我正在使用数据源窗口中的拖放操作,如 这篇 Julie Lerman 帖子。我的情况的不同之处在于,我没有通过数据网格显示和添加详细信息,而是使用 ListView 和文本框供用户添加新的章节标题。

拖放已在 XAML 中创建了两个 CollectionViewSource 条目:

<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}}" />
</Window.Resources>

自动生成的代码如下所示:

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);
    }

我需要向我的 button_click 事件添加哪些代码才能从文本框中获取用户的章节标题并将其添加为新的详细记录通过详细信息的 CollectionViewSource (bookChaptersViewSource)?我本以为这很简单,但我花了太多时间尝试却没有任何运气。提前致谢!

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}}" />
</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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-10 18:55:52

抱歉,VB,但它应该很容易翻译:

Dim c as Chapter = New Chapter
c.Title = ChapterTitleTextBox.Text
context.AddObject(c, "Chapters")
MyBook.Chapters.Add(c)

这就是它的全部内容。创建对象,将其添加到上下文,将其添加到 Master 的详细信息集合中。在上面的示例中,在 Context.AddObject 方法中,“Chapters”指的是要添加到上下文的类的 EntitySet 的名称。

CollectionViewSource 的源设置为 Books Chapter 集合,它应该获取章节的添加,并且 UI 应该使用新元素进行更新。通常,除非更改视图(过滤、排序等),否则您不必与 CollectionViewSource 交互。

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).

暖伴 2024-11-10 18:55:52

我可能不完全理解这个问题,但通常您需要创建一个新的子对象并将其分配给父级的子对象列表。当您保存父对象时,它应该保存新的子对象。
或者您可以分配新子对象的parentid 并保存它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文