Entity Framework "Database First" 没有生成外键

Entity Framework "Database First" not generating Foreign keys

我使用了entity framework逆向工程,但它似乎遗漏了一些外键关系。 这是我的数据库 sql 代码和生成的 类:

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; }
 }
}

和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; }
}
}

那里可能有什么问题?

可能是与以下 linkThe question posed on stack over flow

重复的问题

这里是上面link的相关答案,我想这可能对你有用EF Reveres Engineering

祝你好运

我刚刚结束手动编辑它添加外键注释:

[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; }
 }
}