微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – 实体框架:坚持在多对多添加新实体,而不是重新使用现有的FK

我有很多关系,简要地说
案例—–< CaseSubjectRelationships> —— CaseSubjects

更充分地说
案例(ID,CaseTypeID,…….)
CaseSubjects(ID,displayName,CRMSPIN)
CaseSubjectsRelationships(CaseID,SubjectID,PrimarySubject,RelationToCase,…)

在我的多对多链接表中是与主题与具体情况相关联的附加属性 – 例如开始日期,结束日期,自由文本关系(例如观察者,创建者等)

已创建实体框架数据模型 – ASP.NET 4.0版

我有一个名为CreateNewCase的方法的WCF服务,它接受一个Case对象(由Entity Framework创建的实体)作为其参数 – 它的工作是将案例保存到数据库中。

WCF服务由第三方工具调用。以下是发送的SOAP:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <CreateNewCase xmlns="http://tempuri.org/">
            <c xmlns:a="http://schemas.datacontract.org/2004/07/CAMSModel">
                <a:CaseSubjectsRelationships>
                    <a:CaseSubjectsRelationship>
                        <a:CaseSubject>
                            <a:CRMSPIN>601</a:CRMSPIN>
                            <a:displayName>Fred Flintstone</a:displayName>
                        </a:CaseSubject>
                        <a:PrimarySubject>true</a:PrimarySubject>
                        <a:RelationToCase>Interested</a:RelationToCase>
                        <a:StartDate>2011-07-12T00:00:00</a:StartDate>
                    </a:CaseSubjectsRelationship>
                    <a:CaseSubjectsRelationship>
                        <a:CaseSubject>
                            <a:CRMSPIN>602</a:CRMSPIN>
                            <a:displayName>Barney Rubble</a:displayName>
                        </a:CaseSubject>
                        <a:RelationToCase>Observer</a:RelationToCase>
                        <a:StartDate>2011-07-12T00:00:00</a:StartDate>
                    </a:CaseSubjectsRelationship>
                </a:CaseSubjectsRelationships>
                <a:CaseType>
                    <a:Identifier>Change of Occupier</a:Identifier>
                </a:CaseType>
                <a:Description>Case description</a:Description>
                <a:Priority>5</a:Priority>
                <a:QueueIdentifier>Queue One</a:QueueIdentifier>
                <a:Title>Case title</a:Title>
            </c>
        </CreateNewCase>
    </s:Body>
</s:Envelope>

WCF引擎将它反序列化为一个Case实体,我正确地,当我看到调试器中的一切都正确设置。

我想做的只是创建一个新的CaseSubject,如果数据库中没有指定CRMSPIN的条目(CRMSPIN是中央客户数据库的引用号)

所以,在下面的例子中,我想看看我在CaseSubjects中是否已经有一个CRMSPIN 601的一个条目,如果我这样做,我不想创建另一个(重复的)条目,而是使新的case链接到现在的主题(虽然新行将需要在CaseSubjectsRelationships中创建具体的“附加”信息,如关系等)

这是我试图这样做的.NET代码

Public Class CamsService
    Implements ICamsService

    Public Function CreateNewCase(c As CAMSModel.Case) As String Implements ICamsService.CreateNewCase

        Using ctx As New CAMSEntities
            ' Find the case type '
            Dim ct = ctx.CaseTypes.SingleOrDefault(Function(x) x.Identifier.toupper = c.CaseType.Identifier.toupper)

            ' Give an error if no such case type '
            If ct Is nothing Then
                Throw New CaseTypeInvalidException(String.Format("The case type {0} is not valid.",c.CaseType.Identifier.ToString))
            End If

            ' Set the case type based on that found in database: '
            c.CaseType = ct

            For Each csr In c.CaseSubjectsRelationships
                Dim spin As String = csr.CaseSubject.CRMSPIN
                Dim s As CaseSubject = ctx.CaseSubjects.SingleOrDefault(Function(x) x.CRMSPIN = spin)

                If Not s Is nothing Then
                    ' The subject has been found based on CRMSPIN so set the subject in the relationship '
                    csr.CaseSubject = s
                End If
            Next

            c.CreationChannel = "Web service"
            c.CreationDate = Now.Date

            ' Save it '
            ctx.AddToCases(c)
            ctx.SaveChanges()
        End Using

        ' Return the case reference '
        Return c.ID.ToString
    End Function
End Class

正如你可以看到的,而不是For Each循环,我尝试根据CRMSPIN获取主题,如果我得到一些东西,那么我更新“CaseSubject”实体。 (我也尝试过csr.SubjectID = s.ID,而不是设置整个实体,并且我已经尝试设置它们!

然而,即使在ctx.SaveChanges()行上放置断点,并且查看主题的设置以及在调试器中看到它看起来不错,它总是在CaseSubjects表中创建一个新行。

我原则上可以看到这应该是正常的 – 你会看到我已经完成了相同的事情为Case类型 – 我已经选择了XML中发送的标识符,通过上下文找到具有该标识符的实体,然后更改了该案例。 CaseType到我发现的实体。当它保存时,它的工作原理和预期,没有重复的行。

我只是试图将同样的理论应用于多对多关系的一边。

以下是.edmx的一些(希望相关的)提取

<EntitySet Name="Cases" EntityType="CAMSModel.Store.Cases" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="CaseSubjects" EntityType="CAMSModel.Store.CaseSubjects" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="CaseSubjectsRelationships" EntityType="CAMSModel.Store.CaseSubjectsRelationships" store:Type="Tables" Schema="dbo" />



 <AssociationSet Name="FK_CaseSubjectsRelationships_Cases" Association="CAMSModel.Store.FK_CaseSubjectsRelationships_Cases">
            <End Role="Cases" EntitySet="Cases" />
            <End Role="CaseSubjectsRelationships" EntitySet="CaseSubjectsRelationships" />
          </AssociationSet>
          <AssociationSet Name="FK_CaseSubjectsRelationships_CaseSubjects" Association="CAMSModel.Store.FK_CaseSubjectsRelationships_CaseSubjects">
            <End Role="CaseSubjects" EntitySet="CaseSubjects" />
            <End Role="CaseSubjectsRelationships" EntitySet="CaseSubjectsRelationships" />
          </AssociationSet>

编辑:CaseSubjectsRelationships对象的CaseSubject属性属性设置器:

/// <summary>
/// No Metadata Documentation available.
/// </summary>
<XmlIgnoreAttribute()>
<SoapIgnoreAttribute()>
<DataMemberAttribute()>
<EdmRelationshipNavigationPropertyAttribute("CAMSModel","FK_CaseSubjectsRelationships_CaseSubjects","CaseSubject")>
Public Property CaseSubject() As CaseSubject
    Get
        Return CType(Me,IEntityWithRelationships).RelationshipManager.GetRelatedReference(Of CaseSubject)("CAMSModel.FK_CaseSubjectsRelationships_CaseSubjects","CaseSubject").Value
    End Get
    Set
        CType(Me,"CaseSubject").Value = value
    End Set
End Property

解决方法

你没有指定你使用的上下文模型,所以我假设你使用的是认的(即你没有一些明确的.tt文件生成你的实体)。

所以,基本上这就是我认为正在发生的事情。
在您的代码中,当您从上下文中获取某些内容时:

Dim ct = ctx.CaseTypes.SingleOrDefault(Function(x) x.Identifier.toupper = c.CaseType.Identifier.toupper)

这个ct在上下文中。您从服务(c)中反序列化的方法参数不在上下文中。您可以将上下文视为“对象跟踪和提取”实体,这样可以确保附件中的所有内容都可以了解任何更改(如果是新的,已删除的)。

所以,当你到达这个部分:

' Set the case type based on that found in database: '
  c.CaseType = ct

在分配一些附加到不附加的东西的时刻,未连接的对象也将被拉入上下文中 – 不能有“部分”附加的实体 – 如果附加的实体,它引用的所有内容也必须被附加。所以,这是c被“拖”到上下文(隐含的)的时刻。当它进入上下文时,它将被标记为“新”,因为它不知道任何东西(它不知道它,没有更改跟踪信息…)。

所以,现在关于该对象c的所有内容都是在上下文中,当您查询上下文时:

Dim s As CaseSubject = ctx.CaseSubjects.SingleOrDefault(Function(x) x.CRMSPIN = spin)

它会显示,确实有一个对象与CRMSPIN,它已经附加 – “嘿,没有必要去数据库,我已经有了! (试图聪明,避免db命中),它将返回您自己的对象。

最后,当您保存所有内容时,它将被保存,但是将附加的c和标记为“new”的所有子对象都将被插入,而不是更新。

最简单的修复将是首先从上下文查询所需的内容,然后才开始将其分配给对象的属性。另外,看看UpdateCurrentValues,也可能有帮助…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WPF...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这...
Some samples are below for ASP.Net web form controls:(from http://www.visualize.uk.com/resources/asp
问题描述: 对于未定义为 System.String 的列,唯一有效的值是(引发异常)。 For columns not defined as System.String, the only vali
最近用到了CalendarExtender,结果不知道为什么发生了错位,如图在Google和百度上找了很久,中文的文章里面似乎只提到了如何本地化(就是显示中文的月份)以及怎么解决被下拉框挡住的问题,谈
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence changed a lot since 1.1. Here is the order: App
静态声明: &#39; Style=&quot;position: relative&quot; AppendDataBoundItems=&quot;True&quot;&gt; (无 或 空 或
以下内容是从网络上搜集资料,然后整理而来的。不当之处,请不吝指教。(The following were from network, and edited by myself. Thanks in a
Imports System Imports System.Reflection Namespace DotNetNuke &#39;*********************************
Ok so you have all seen them: “8 million tools for web development”, “5 gagillion tools that if you