繁体   English   中英

实体框架“数据库优先”未生成外键

[英]Entity Framework “Database First” not generating Foreign keys

我使用实体框架逆向工程,但似乎错过了一些外键关系。 这是我的数据库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; }
}
}

那里可能是什么问题?

带有以下链接重复问题

在这里,我想上述链接的相关答案对您很有价值EF Reveres Engineering

祝好运

我刚结束手动编辑它,添加了forgein键注释:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM