0

我遇到了一个真正的问题,我知道这是因为我知道的不够多。我已经搜索了大约 50 篇文章,但找不到答案。

这是我的域模型

Namespace Models
    Public Class Article
        Public Property ArticleId As Integer
        Public Property Author As String

        <DisplayFormat(DataFormatString:="{0:D}")>
        Public Property CreatedOn As DateTime

        <DisplayFormat(DataFormatString:="{0:D}")>
        Public Property LastModified As DateTime

        <AllowHtml()>
        Public Property Content As String
        Public Property Title As String
        Public Property Excerpt As String
        Public Property IsPublic As Boolean

        Public Overridable Property Category As ICollection(Of Category)
        Public Overridable Property Tags As ICollection(Of Tag)
        Public Overridable Property Status As ICollection(Of Status)
        Public Overridable Property Comments As ICollection(Of Comment)
    End Class
End Namespace


Namespace Models
    Public Class Status
        Public Property StatusId As Integer
        Public Property Name As String

        Public Overridable Property Articles As ICollection(Of Article)
    End Class
End Namespace


Namespace Models
    Public Class Category
        Public Property CategoryId As Integer
        Public Property Name As String

        Public Overridable Property Articles As ICollection(Of Article)
    End Class
End Namespace

Namespace Models
    Public Class Tag
        Public Property TagId As Integer
        Public Property Name As String

        Public Overridable Property Articles As ICollection(Of Article)
    End Class
End Namespace

这是我的 ViewModel(基于我的域模型)

Namespace ViewModels.Admin
    Public Class ArticleViewModel
        Public Property Article As Article
        Public Property CategoryId As Integer
        Public Property StatusId As Integer
        Public Property Tags As ICollection(Of Tag)
    End Class
End Namespace

我不知道如何保存所有这些数据交叉模式。我现在尝试了很多方法,我不知道在哪里看。我已经尝试了几本书,但没有人真正深入研究这一点。我还查看了 Contoso 大学项目,但它似乎不像我的项目。任何人都可以帮忙吗?

4

1 回答 1

1

如果您使用实体框架来执行您的 DAL,您可以很容易地做到这一点。只需从上到下构建对象。因此,在您的示例中,从 ViewModel 获取文章并从 View Model 的其他部分填充其导航属性。

不要担心连接子对象的导航属性。

然后,您可以使用文章集合上的 AddObject 将这篇文章添加到上下文中,并且所有子属性也应该插入并连接起来。

当心 Db 中可能已经存在的子属性,我看到 EF 会在这些项目上插入,即使它们有 Id,而不仅仅是更新映射表等。

于 2012-09-03T08:10:42.390 回答