提问人:Nono-Man 提问时间:9/17/2023 最后编辑:Dale KNono-Man 更新时间:9/17/2023 访问量:37
存储将对象列表作为属性的对象,而不让这些对象生成单独的表
Store object that has a List of objects as a property, without these objects generating a seperate table
问:
我有这个具有 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);
});
我觉得这应该是一件非常简单的事情,但我看不到正确的方法。提前致谢!
答: 暂无答案
评论
FieldStructure.Name