none
Mapping 1:0 without FK RRS feed

  • Question

  • have a quite old Database, one details have 0 or 1 trainer. 

    TABLE [dbo].[DETAILS] (
        [SEDETID]       INT          IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,

    TABLE [dbo].[TRAINER] (
        [SemTrainID]      INT          IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,
        [SEDETID]         INT          DEFAULT ((0)) NOT NULL,

    should map with SEDETID, maps with semtrainid->sedetid

    Have in both classes virtual (overrideable) propertys

    and in Dbcontext class a more conceptual code, how to map the correct columns=

            modelBuilder.Entity(Of DETAILS)() _
                .HasOptional(Function(e) e.Trainer) _
                        .WithOptionalDependent(Function(f) f.details) _
            .Map(Function(g) g.MapKey("SEDETID"))

            modelBuilder.Entity(Of TRAINER)() _
                .HasOptional(Function(e) e.details) _
            .WithRequired(Function(f) f.Trainer) _
            .Map(Function(g) g.MapKey("SEDETID"))

    Thanks Hannes

    Saturday, September 22, 2018 5:46 PM

All replies

  • Hi HannesPreishuber,

    If it is a one-to-zero or one relationship, I would suggest that you could config your model class like this:

    Public Class DETAILS
        <Key>
        Public Property SEDETID As Integer
        Public Overridable Property TRAINER As TRAINER
    End Class
    
    Public Class TRAINER
        <Key>
        <ForeignKey("DETAILS")>
        Public Property SemTrainID As Integer
        Public Property SEDETID As Integer
        Public Overridable Property DETAILS As DETAILS
    End Class
    

    For more information about entity framework one-to-one relationship, please refer to:

    http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 25, 2018 2:59 AM
    Moderator