[英]ManyToMany Relation → Add a value to the junction table
I have two entities: Component and Attribute.我有两个实体:组件和属性。 Components can have multiple Attributes and Attributes can also have multiple Components.
组件可以有多个属性,属性也可以有多个组件。 The value of the attribute will be stored in the junction table "ComponentAttribute".
属性的值将存储在连接表“ComponentAttribute”中。 I don't know whats the best way to add the value.
我不知道增加价值的最佳方法是什么。
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
So it would be nice, if someone could give me a hint how to access the value所以这会很好,如果有人能给我一个提示如何访问该值
Unfortunately, you cannot do this.不幸的是,您不能这样做。
SQLite-Net-Extensions uses InsertAll
method inside Many-To-Many relationships handler and inserting only that properties which marked as ForeignKey
into database table. SQLite-Net-Extensions 在多对多关系处理程序中使用
InsertAll
方法,并仅将标记为ForeignKey
属性插入到数据库表中。
Best workaround is that you using in question.最好的解决方法是您使用有问题的方法。
Load ComponentAttributes
entity from database by known data ( Attribute.id
and Component.id
) and then update value
of recieved ComponentAttribute
object by primary key ( ComponentAttribute.id
).通过已知数据(
Attribute.id
和Component.id
)从数据库加载ComponentAttributes
实体,然后通过主键( ComponentAttribute.id
)更新收到的ComponentAttribute
对象的value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.