Entity Framework - strange problem with multiple foreign keys mapped to one table

I use EF (Framework 3.5 SP1) and create a simple demonstration of two tables:

Candidates

  • int id
  • request-pref-lang-coorepondence int (FK to CodeLanguages)
  • application-pref-lang-exam int (FK to CodeLanguages)
  • request-pref-lang-interview int (FK for CodeLanguages)

CodeLanguages

  • code-lang-id int
  • code-lang-desc varchar

CodeLanguage Entries May Have 0, 1, * Applicants

In each language indicated in the application, 1 (and only one) must be indicated. Link to CodeLanguage.

: - ( - WCF WPF), (coorespondence, exam interview) , 1000 (), , , 1001 (), 1001 ().

: (, coorespondence = , = = ), - , , , , - .

, , EDMX, EDMX - . - , EF, .

? .

+2
2

. EF Microsoft , EF 3.5 SP1:

" , , , , CodeLanguage. WCF , , , "

Microsoft , :

: Partial Class Applicants , 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

-: , :

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()
+1

, , .

, , .

, .

, (alexj) @microsoft.com.

Entity Framework Team

0

Source: https://habr.com/ru/post/1765694/


All Articles