[英]ManyToMany Relation → Add a value to the junction table
我有两个实体:组件和属性。 组件可以有多个属性,属性也可以有多个组件。 属性的值将存储在连接表“ComponentAttribute”中。 我不知道增加价值的最佳方法是什么。
public class Component
{
[PrimaryKey, AutoIncrement]
public int id { get; set; }
[ManyToMany(typeof(ComponentAttribute))]
public List<Attribute> attributes { get; set; }
}
public class Attribute
{
[PrimaryKey, AutoIncrement]
public int id { get; set; }
public string name { get; set; }
[ManyToMany(typeof(ComponentAttribute))]
public List<Component> components { get; set; }
}
public class ComponentAttribute
{
[PrimaryKey, AutoIncrement]
public int id { get; set; }
public string value { get; set; }
[ForeignKey(typeof(Component))]
public int componentId { get; set; }
[ForeignKey(typeof(Attribute))]
public int attributeId { get; set; }
}
Attribute attribute = new Attribute();
Attribute attribute2 = new Attribute();
Component component = new Component();
component.attributes.Add(attribute);
component.attributes.Add(attribute2);
this.dbConnection.Insert(attribute);
this.dbConnection.Insert(attribute2);
this.dbConnection.Insert(component);
this.dbConnection.UpdateWithChildren(component);
// After this I have to load the new ComponentAttributes and add the value manual
所以这会很好,如果有人能给我一个提示如何访问该值
不幸的是,您不能这样做。
SQLite-Net-Extensions 在多对多关系处理程序中使用InsertAll
方法,并仅将标记为ForeignKey
属性插入到数据库表中。
最好的解决方法是您使用有问题的方法。
通过已知数据( Attribute.id
和Component.id
)从数据库加载ComponentAttributes
实体,然后通过主键( ComponentAttribute.id
)更新收到的ComponentAttribute
对象的value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.