存储将对象列表作为属性的对象,而不让这些对象生成单独的表

Store object that has a List of objects as a property, without these objects generating a seperate table

提问人:Nono-Man 提问时间:9/17/2023 最后编辑:Dale KNono-Man 更新时间:9/17/2023 访问量:37

问:

我有这个具有 2 个复杂属性的类:

public class App
{
    [Key]
    public Guid Id { get; set; }
    public string StringId { get; set; } = string.Empty;
    public string AppName { get; set; } = string.Empty;
    public Guid WorkspaceId { get; set; }
    public List<FieldStructure> Fields { get; set; } = new List<FieldStructure>();
    public List<FieldStructureRelation> RelationFields { get; set; } = new List<FieldStructureRelation>();
}

这些是所谓的属性对象:

[Owned]
public class FieldStructure
{
    public FieldStructure(string name, string type)
    {
            Name = name;
            Type = type;
            NameAsProperty = name.ToLower().Replace(" ", "_");
            
    }

    public FieldStructure() { }

    public string Name { get; set; } = string.Empty;
    public string NameAsProperty { get; set; } = string.Empty;
    public string Type { get; set; } = string.Empty;

}
[Owned]
public class FieldStructureRelation : FieldStructure
{
    public FieldStructureRelation(FieldStructure fieldStructure, string appRelationId) 
        : base(fieldStructure.Name, fieldStructure.Type)
    {
        this.AppRelationId = appRelationId;
    }

    public FieldStructureRelation(string name, string type, string appRelationId) 
        : base(name, type) 
    {
        this.AppRelationId = appRelationId;
    }
        
    public FieldStructureRelation() { }

    public string AppRelationId { get; set; } = string.Empty;
}

我想要的只是将这些属性存储在我的应用程序表中的列中,而不是存储在单独的表中......也许作为JSON对象?

我以为在我的 dbcontext 中这样做就足够了,但是不,它会不断生成表并且不显示我的应用程序中的属性:

modelBuilder.Entity<App>()
    .OwnsMany(app => app.Fields, fields =>
    {
        fields.Property(c => c.Type);
        fields.Property(c => c.Name);
        fields.Property(c => c.NameAsProperty);
    });
modelBuilder.Entity<App>()
    .OwnsMany(app => app.RelationFields, rFields =>
    {
        rFields.Property(c => c.Type);
        rFields.Property(c => c.Name);
        rFields.Property(c => c.NameAsProperty);
        rFields.Property(c => c.AppRelationId);
    });

我觉得这应该是一件非常简单的事情,但我看不到正确的方法。提前致谢!

asp.net . SQL-Server Entity-Framework

评论

0赞 Monofuse 9/17/2023
尝试 ICollection 而不是 List
0赞 Nono-Man 9/17/2023
@Monofuse同样的事情发生了......出于某种原因,et 甚至在他们的表中生成了一个 Id 字段
0赞 Guru Stron 9/17/2023
想象一下,您需要为此类生成表(如果要将其存储为一个表)。应该生成多少列?一?二?一百万?无法判断列表中将有多少元素。FieldStructure.Name

答: 暂无答案