簡體   English   中英

ManyToMany Relation → 給連接表添加一個值

[英]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.idComponent.id )從數據庫加載ComponentAttributes實體,然后通過主鍵( ComponentAttribute.id )更新收到的ComponentAttribute對象的value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM