0

I'm using E.F db first and I have two tables :

User details :

CREATE TABLE [dbo].[detailsUtilisateur] 
(
    [idDetailsUtilisateur] INT          IDENTITY (1, 1) NOT NULL,
    [nomUtilisateur]       VARCHAR (30) NULL,
    [prenomUtilisateur]    VARCHAR (30) NULL,
    [compagnieNom]         VARCHAR (30) NULL,
    [noTelephone]          NCHAR (10)   NULL,
    [adresse]              VARCHAR (40) NULL,
    [ville]                VARCHAR (30) NULL,
    [pays]                 VARCHAR (30) NULL,
    [etat]                 VARCHAR (30) NULL,
    [codePostal]           NCHAR (6)    NULL,

    CONSTRAINT [pk_idDetailsUtilisateur] 
        PRIMARY KEY CLUSTERED ([idDetailsUtilisateur] ASC),
    CONSTRAINT [fk_idDetailsUtilisateur] 
        FOREIGN KEY ([idDetailsUtilisateur]) REFERENCES [dbo].[Utilisateur] ([idUtilisateur])
);

User

CREATE TABLE [dbo].[Utilisateur] 
(
    [idUtilisateur] INT          IDENTITY (1, 1) NOT NULL,
    [email]         VARCHAR (30) NOT NULL,
    [mdp]           VARCHAR (90) NOT NULL,

    CONSTRAINT [pk_utilisateur] 
        PRIMARY KEY CLUSTERED ([idUtilisateur] ASC),
    UNIQUE NONCLUSTERED ([email] ASC)
);

I'm trying to add a user, then his details. But I get an error:

Referential mapping on the column idDetailsUtilisateur.

I don't understand because the table detailsUtilisateur had a column idDetailsUtilisateur which is a primary key and a foreign key, why did I get this error? Thank you

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
N.Farouk
  • 21
  • 9
  • Please always show the exact original error message – Sami Kuhmonen Apr 06 '17 at 04:44
  • I fixed the problem by removing the identity(1,1) on detailsUtilisateur table, but I don't understand why I needed to remove this ? Can someone explain me this? – N.Farouk Apr 06 '17 at 04:55
  • I have a doubt why you set the same field as primary key as well as foreign key in user details table, as foreign key always allow duplicates and primary refers to uniqueness... – divya-sarojam Apr 06 '17 at 05:04
  • First, this is not truly a 1 to 1 relationship. It's a 1 to 0..1 relationship, because you can create a Utilisateur row without a corresponding detailsUtilisateur row. However, you cannot create a detailsUtilisateur row without previously having a matching Utilisateur row. Second, if the rows are out of sequence then the identities generated will be different and one will not exist. – Erik Funkenbusch Apr 06 '17 at 05:05
  • I think the following links will help you [http://stackoverflow.com/questions/18680680/can-foreign-key-refer-to-primary-key-in-same-table](http://stackoverflow.com/questions/18680680/can-foreign-key-refer-to-primary-key-in-same-table) [http://stackoverflow.com/questions/10982992/is-it-fine-to-have-foreign-key-as-primary-key](http://stackoverflow.com/questions/10982992/is-it-fine-to-have-foreign-key-as-primary-key) – divya-sarojam Apr 06 '17 at 05:15

0 Answers0