Entity Framework "Database First" not generating Foreign keys

asp.net c# entity-framework-6 visual-studio-2015

Question

I used entity framework reverse engineering but it seems to miss some foreign key relations. Here are my database sql code and the generated classes:

CREATE TABLE [dbo].[rapportAnomalie] (
[code_rapport] INT          IDENTITY (1, 1) NOT NULL,
[date_rapport] DATETIME     NOT NULL,
[etat]         VARCHAR (50) NOT NULL,
[code_agence]  INT          NOT NULL,
PRIMARY KEY CLUSTERED ([code_rapport] ASC),
CONSTRAINT [FK_rapportAnomalie_ToTable] FOREIGN KEY ([code_agence]) REFERENCES [dbo].[agence] ([code_agence])


CREATE TABLE agence
(
[code_agence] INT NOT NULL PRIMARY KEY, 
[intitule_agence] VARCHAR(100) NOT NULL, 
[adresse_agence] VARCHAR(100) NOT NULL
)

Agence.cs:

[Table("agence")]
public partial class agence
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int code_agence { get; set; }

    [Required]
    [StringLength(100)]
    public string intitule_agence { get; set; }

    [Required]
    [StringLength(100)]
    public string adresse_agence { get; set; }
 }
}

and Rapport.cs:

[Table("rapportAnomalie")]
public partial class rapportAnomalie
{
    [Key]
    public int code_rapport { get; set; }

    public DateTime date_rapport { get; set; }

    [Required]
    [StringLength(50)]
    public string etat { get; set; }

    public int code_agence { get; set; }
}
}

what could be the problem there?

1
0
10/21/2016 8:30:03 PM

Accepted Answer

I just ended up editing it manually adding the forgein key annotation:

[Table("rapportAnomalie")]
public partial class rapportAnomalie
 {
    [Key]
    public int code_rapport { get; set; }

    public DateTime date_rapport { get; set; }

    [Required]
    [StringLength(50)]
    public string etat { get; set; }

    public int code_agence { get; set; }
    [ForeignKey("code_agence")]
    public agence agence { get; set; }
 }
}
0
7/8/2015 11:02:21 AM

Popular Answer

Probably a duplicate question with the following linkThe question posed on stack over flow

And here the related answer of the above link which I guess could be of value for youEF Reveres Engineering

Good luck



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow