none
How to create a 1:1 relationship? RRS feed

  • Question

  • I am using the database first process to create my edmx model in EF.

    I have two tables:

    TableA(IDA, ...)

    TableB(IDB, IDA, ...)

    I create the relationship between both tables and create an index in the TableB in the IDA field, and I set it as unique. When I create my edmx model, the relationship is 1:N.

    I know that if I use in TableB only IDA as primary key then the relationship is N:N but in this case I would like to have a ID for TableB.

    Is it possible to create a 1:1 realtionship in EF in this case?

    Thanks so much.

    • Moved by CoolDadTx Friday, April 24, 2015 6:05 PM EF related
    Friday, April 24, 2015 4:03 PM

Answers

  • Hello ComptonAlvaro,

    >>Is it possible to create a 1:1 realtionship in EF in this case?

    Yes, you can. Entity Framework is a ORM framework, it is based on the database, so only if you create a one to one relationship between table A and table B, it would create a one to one relationship. For creating one to one relationship in database(assuming you are using sql server database), you could create it as below:

    CREATE TABLE [dbo].[Users] (
    
    [UserID] UNIQUEIDENTIFIER CONSTRAINT [DF_Users_UserID] DEFAULT (newid()) NOT NULL,
    
    [Name] NVARCHAR (50)    NOT NULL,
    
    CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([UserID] ASC),
    
    );
    
    CREATE TABLE [dbo].[UserDetails] (
    
    [UserID]           UNIQUEIDENTIFIER NOT NULL,
    
    [Address]             NVARCHAR(100) NOT NULL,
    
    CONSTRAINT [PK_UserDetails] PRIMARY KEY CLUSTERED ([UserID] ASC),
    
    CONSTRAINT [FK_UserDetails_Users] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID]) ON DELETE CASCADE 
    
    );
    

    Setting the primary key of the child table to be both primary key and foreign key.

    Or you could have a try with approaches provided by IWolbers, using code first approach which is easier to configure the relationship.

    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, April 27, 2015 1:52 AM
    Moderator

All replies

  • Here is how to do it with EF code first using either fluent notation or annotations.

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

    Here is how to do it in the edmx designer.

    https://msdn.microsoft.com/en-us/data/jj713299.aspx


    Mark as answer or vote as helpful if you find it useful | Igor

    Friday, April 24, 2015 5:00 PM
  • Hello ComptonAlvaro,

    >>Is it possible to create a 1:1 realtionship in EF in this case?

    Yes, you can. Entity Framework is a ORM framework, it is based on the database, so only if you create a one to one relationship between table A and table B, it would create a one to one relationship. For creating one to one relationship in database(assuming you are using sql server database), you could create it as below:

    CREATE TABLE [dbo].[Users] (
    
    [UserID] UNIQUEIDENTIFIER CONSTRAINT [DF_Users_UserID] DEFAULT (newid()) NOT NULL,
    
    [Name] NVARCHAR (50)    NOT NULL,
    
    CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([UserID] ASC),
    
    );
    
    CREATE TABLE [dbo].[UserDetails] (
    
    [UserID]           UNIQUEIDENTIFIER NOT NULL,
    
    [Address]             NVARCHAR(100) NOT NULL,
    
    CONSTRAINT [PK_UserDetails] PRIMARY KEY CLUSTERED ([UserID] ASC),
    
    CONSTRAINT [FK_UserDetails_Users] FOREIGN KEY ([UserID]) REFERENCES [dbo].[Users] ([UserID]) ON DELETE CASCADE 
    
    );
    

    Setting the primary key of the child table to be both primary key and foreign key.

    Or you could have a try with approaches provided by IWolbers, using code first approach which is easier to configure the relationship.

    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, April 27, 2015 1:52 AM
    Moderator
  • Thanks Fred, but in my case the Child table has it's own primary key, and also has the foreign key. So if I set the the primary key of my child table as primary key and as foreign key, I don't have the correct relationship.

    I mean, supose that I have as PK on parent table the value 123. In my child table, I have as PK the value 111, so in my FK I need to set 123 as value, because is the PK of the parent table.

    This is because in my child table I would like to have a autonumeric as PK, so the PK can be different to the PK of the main table.

    Tuesday, May 5, 2015 9:49 AM