none
1-0 or 1 relationship Entity Framework Database First RRS feed

  • Question

  • I try to set a 1 to 0..1 relation in EDMX model.

    The Model is generate by Database first.

    Table A :

    id_a

    Table B :

    id_b

    id_a

    With Foreign Key B.id_a = A.id_a and unique constraint B.id_a is Unique

    EF Model give me error :

    Dependent Role must be *

    How can I define 1 to 0 or 1 with Database first ?


    Friday, May 23, 2014 11:53 AM

Answers

  • Hello David,

    >>With Foreign Key B.id_a = A.id_a and unique constraint B.id_a is Unique

    This is used to define 1 to many relationship rather 1 to 0…1 relationship, since you are working with database first in entity framework, you need to define the 1 to 0…1 relationship in database as:

    CREATE TABLE [dbo].[Course]
    
    (
    
           [CourseID] INT NOT NULL PRIMARY KEY, 
    
        [CourseName] NVARCHAR(50) NOT NULL
    
    )
    
    CREATE TABLE [dbo].[OnlineCourse]
    
    (
    
           [CourseID] INT NOT NULL PRIMARY KEY, 
    
        [CourseName] NVARCHAR(50) NULL,
    
           CONSTRAINT [FK_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) ON DELETE CASCADE
    
    )
    

    The result:

    Database "always" uses 1 - 0..1 because real 1 - 1 cannot be effectively used for data insertion. 1 - 1 means that left must be inserted after right but right must be inserted after left = impossible. Because of that 1 - 0..1 is used where left is principal and must be inserted before right and right is dependent and must be inserted after left.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 26, 2014 1:53 AM
    Moderator