实体框架 - 将多个外键映射到同一个表的奇怪问题 [英] Entity Framework - strange issue with multiple foreign keys mapped to the same table

查看:105
本文介绍了实体框架 - 将多个外键映射到同一个表的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是EF(Framework 3.5 SP1),并且安装了一个简单的两个桌面演示:

I am using EF (Framework 3.5 SP1) and have a simple two table demo set up:

申请人


  • 申请人id int

  • 申请人-first-lang-coorepondence int(FK to CodeLanguages)

  • 申请人-pref-lang-exam int(FK to CodeLanguages)

  • 申请者-Pre-lang-interview int(FK to CodeLanguages)

  • applicant-id int
  • applicant-pref-lang-coorepondence int (FK to CodeLanguages)
  • applicant-pref-lang-exam int (FK to CodeLanguages)
  • applicant-pref-lang-interview int (FK to CodeLanguages)

CodeLanguages

CodeLanguages


  • code-lang-id int

  • 代码-lang-desc varchar

CodeLanguage条目可以有0,1,*申请人

A CodeLanguage entry can have 0, 1, * Applicants

申请人中的每种语言参考文献必须具有1(并且只有一个)CodeLanguage参考。

Each language ref in Applicants must have 1 (and only one) CodeLanguage ref.

问题:
当我带回申请人实体WCF Web服务到我的基于WPF的客户端),如果所有三种语言参考(coorespondence,exam,and interview)都是一样的,可以说1000(英文),然后修改其中的一个,例如1001(法语),那么所有三个将被更改为1001(法语)

Problem: When I bring back an Applicant entity (via WCF web service to my WPF-based client), if all three language references (coorespondence, exam, and interview) are all the same, lets say 1000 (english), and then I modify one of them, for example to 1001 (french), then all THREE will be changed to 1001 (french).

这是奇怪的部分:如果所有三个参考都不同(让我们说coorespondence = english,exam = french,and interview =西班牙语),我改变其中一个 - 那么它的行为就像预期的那样,只有我改变的那个被影响 - 其他人仍然处于原始状态。

Here's the weird part: If all three references are different (lets say coorespondence=english, exam=french, and interview=spanish) and I change one of them - then it behaves as expected and only the one I changed is affected - the others are remain in their original state.

我已经花了大部分时间来尝试各种各样的东西,如在EDMX中删除和重新创建关联,重新创建EDMX数据模型 - 甚至创建一个新的数据库。没有一个工作 - 我开始的事情是与EF而不是我的代码。

I have spent most of today trying various things such as dropping and recreating associations in the EDMX, recreating the EDMX datamodel - even creating a new database. None of this worked - I'm beginning to thing the issue is with EF and not my code.

任何想法?谢谢。

推荐答案

关于此问题的最终结果的更新。经过微软EF团队的一些非常快速有用的建议后,确定这是EF 3.5 SP1的预期行为:

An update on the final outcome of this issue. After some very quick and helpful advice from the EF team at Microsoft it was determined that this is expected behaviour from EF 3.5 SP1:

当您在所有语言相同的申请人的服务层,最终都有两个对象,一个具有三个导航属性的申请人指向相同的CodeLanguage对象。然后,WCF在客户端上重新创建相同的图形,意味着您设置的三个断点确实在同一个对象上查看相同的财产

微软为我的最终解决方案提供了基础:

Microsoft provided the basis for my ultimate solution which is this:

首先:为申请人数据对象创建一个部分类,并创建引用三种语言code_ids的三个属性:

First: Create a Partial Class for the Applicants data object and create three properties which reference the three language code_ids:

部分公共类申请人

Private _intPrefCoorespLanguage As Integer = 0

Private _intPrefInterviewLanguage As Integer = 0

Private _intPrefExamLanguage As Integer = 0

<System.Runtime.Serialization.DataMemberAttribute()> _
Public Property MyPrefCoorespLanguageCodeId() As Integer
    Get
        Return (_intPrefCoorespLanguage)
    End Get
    Set(ByVal value As Integer)
        _intPrefCoorespLanguage = value
    End Set
End Property

<System.Runtime.Serialization.DataMemberAttribute()> _
Public Property MyPrefInterviewLanguageCodeId() As Integer
    Get
        Return (_intPrefInterviewLanguage)
    End Get
    Set(ByVal value As Integer)
        _intPrefInterviewLanguage = value
    End Set
End Property

<System.Runtime.Serialization.DataMemberAttribute()> _
Public Property MyPrefExamLanguageCodeId() As Integer
    Get
        Return (_intPrefExamLanguage)
    End Get
    Set(ByVal value As Integer)
        _intPrefExamLanguage = value
    End Set
End Property


<OnSerializing()> _
Private Sub PopulateClientProperties(ByVal sc As StreamingContext)
    Me.MyPrefCoorespLanguageCodeId = Me.PrefCoorespLanguage.code_lang_id
    Me.MyPrefInterviewLanguageCodeId = Me.PrefInterviewLanguage.code_lang_id
    Me.MyPrefExamLanguageCodeId = Me.PrefExamLanguage.code_lang_id
End Sub

结束类

第二:重新编译和刷新客户端的服务引用。使用三种语言code_id属性绑定到xaml中的控件

Second: Recompile and refresh the client's service reference. Use the three language code_id properties to bind to controls in xaml

第三:在服务器端更新中,运行以下更新应用程序和其语言外键:

Third: In the server-side update run the following to update the applciant and its language foreign keys:

myContext = New HR2009Entities

'Get original Applicant and feed in changes from detatched updated Applicant object
Dim OrigApp = (From a In myContext.Applicants Where a.applicant_id = pobjUpdatedApplicant.applicant_id Select a).First

'Apply preferred language foreign key refs
OrigApp.PrefCoorespLanguageReference.EntityKey = _
   New EntityKey("HR2009Entities.CodeLanguages", "code_lang_id",pobjUpdatedApplicant.MyPrefCoorespLanguageCodeId)

OrigApp.PrefInterviewLanguageReference.EntityKey = _
   New EntityKey("HR2009Entities.CodeLanguages", "code_lang_id", pobjUpdatedApplicant.MyPrefInterviewLanguageCodeId)

OrigApplicant.PrefExamLanguageReference.EntityKey = _
   New EntityKey("HR2009Entities.CodeLanguages", "code_lang_id", pobjUpdatedApplicant.MyPrefExamLanguageCodeId)

'Apply Applicant table native-field changes
myContext.ApplyPropertyChanges(OrigApp.EntityKey.EntitySetName, pobjUpdatedApplicant)

'Save to database
myContext.SaveChanges()

myContext.Dispose()

这篇关于实体框架 - 将多个外键映射到同一个表的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆