[英]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; }
}
}
那里可能是什么问题?
我刚结束手动编辑它,添加了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.