实体框架6与system.componentmodel.Annotations(网络标准兼容)程序集的数据注释不兼容



我正在尝试将实体框架与一组定义为net standard2.0 nuget包一部分的实体一起使用。使用Entity框架的项目是一个net472项目。当我在测试中构建模型时,我看到以下错误

System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
CodeFirstNamespace.<TestEntity>: : EntityType 'TestEntity' has no key defined. Define the key for this EntityType.
TestEntities: EntityType: EntitySet 'TestEntity' is based on type 'TestEntity' that has no keys defined.

上面的错误表明其中一个实体没有定义键,但定义了键属性。以下是实体定义

[Table("TestEntity")]
[FilterSupported]
internal sealed class TestEntity
{
[Key]
[Column("entity1Id", Order = 1)]
public int TestId{ get; set; }
public RelatedEntity entity1Id{ get; set; }
[Key]
[Column("entity2", Order = 2)]
public int entity2Id { get; set; }
public RelatedEntity2 entity2 { get; set; }
}

以下是项目中使用的参考资料

  • 实体框架6
  • System.ComponentModel.Annotations 4.7(用于我们的实体nuget所需的网络标准兼容性(
  • 实体dll(网络标准2.0 nuget(

这个定义一直有效,直到";System.ComponentModel.DataAnnotations";被替换为";System.ComponentModel.Annotations";。

有什么方法可以解决这个问题吗?或者我应该使用实体核心,因为它支持点网标准吗?

提前感谢

在.NET 4.7.2项目中,删除System.ComponentModel.DataAnnotations引用,转而引用相同的Nuget包(System.ComponentModel.Annotations(。您可能需要添加绑定重定向。

最新更新