gpt4 book ai didi

c# - Entity Framework 代码优先 : How to map flat table to class with nested objects

转载 作者:太空狗 更新时间:2023-10-29 17:49:42 27 4
gpt4 key购买 nike

我有一个场景,其中来自单个表的数据必须在 2 个对象中。

[Table]
-Field1
-Field2
-Field3
-Field4

类看起来像这样:

[Class1]
-Field1
-Field2
-Class2 object here

[Class2]
-Field3
-Field4

我在 Class1 中设置了包含字段 3 和 4 的 Class2 属性的 [NotMapped] 属性。我还在数据库上下文中添加了配置:

public class ConfigurationClass1 : EntityTypeConfiguration<Class1> {
public ConfigurationClass1 () {
Property(o => o.Class2.Field3).HasColumnName("Field3");
Property(o => o.Class2.Field4).HasColumnName("Field4");
}
}

问题是,当我尝试将 Entity Framework 与 Class1 一起使用时,我得到了:

The property 'Class2' is not a declared property on type 'Class2'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.

如何将 Entity Framework Code First 用于具有嵌套对象且所有信息都在平面表中的实体?

最佳答案

只有在 Class2 可以被 EF CF 识别为 Complex type 的情况下,您才能这样做.

简要说明:

  1. Class2 不应包含对其他 EF 实体的任何引用。仅有的到其他复杂类型或标准类型
  2. Class2 不能是通用的。在这种情况下,作为解决方法,您可以创建一个非泛型嵌套类并在您的 Class1 中使用它。

关于c# - Entity Framework 代码优先 : How to map flat table to class with nested objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11018794/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com