我有一个现有的API,现在它使用的数据库中有表,这些表必须有一个复合键。我使用fluent API来定义dbcontext中的键。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//relationship modeling
modelBuilder.Entity<Reports>().HasMany(r => r.Screenshots).WithOne().HasForeignKey(r => new { r.ReportId, r.itemTypeId });
modelBuilder.Entity<Images>().HasOne(s => s.Reports).WithMany(s => s.Screenshots).HasForeignKey(s => new { s.ReportId, s.itemTypeId });
modelBuilder.Entity<EpicReportsChildList>().HasOne(e => e.Reports).WithMany(e => e.EpicReports).HasForeignKey(e => new { e.TemplateId, e.parentTypeId });
modelBuilder.Entity<Images>().HasOne(s => s.EpicReports).WithMany(s => s.Screenshots).HasForeignKey(s => new { s.ReportId, s.itemTypeId });
}
我有下面的启动文件:
private IEdmModel GetEdmModel()
{
var oDatabuilder = new ODataConventionModelBuilder();
oDatabuilder.EntitySet<Reports>("reports");
return oDatabuilder.GetEdmModel();
}
API返回以下错误:
实体类型EpicReportsChildList
具有用数据注释定义的复合主键。若要设置复合主键,请使用fluent API。
发布于 2020-08-30 02:03:20
实体类型EpicReportsChildList具有用数据注释定义的复合主键。若要设置复合主键,请使用fluent API。
正如错误消息所述,也许您正在使用数据注释或fluent API方法配置复合主键。
在EF核心密钥文件中,我们可以发现:组合键只能使用Fluent API进行配置;约定永远不会设置复合键,也不能使用数据注释来配置.。
因此,请检查相关模型并尝试删除数据注释:[Key]
属性或[ForeignKey]
attibute。
有关使用组合键配置关系的更多详细信息,您可以查看以下文章(将多到多个关系连接表配置为使用复合主键,您可以参考它们):
https://stackoverflow.com/questions/63632558
复制相似问题