2017-09-11 17 views
0

Xamarinを使用したアプリケーションの開発にSQLite-Net PCLとSQLite-Net拡張を併用しています。2つのエンティティ間のSQLite-Netエクステンション多対多の関係

私のモデルでは、私は2つのエンティティを持っています。私たちは商品と売り手を多対多の関係で結ばれています。例えば、製品は多くの売り手によって販売され、各売り手は多くの製品を販売することができる。

この関係を構築するために別のモデルを作成する必要はありますか? ProductIdは製品の主キーであり、売り手の外部キーです。

がここに以下の私のモデルである:

public class Product 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [PrimaryKey, AutoIncrement] 
    public int ProductID { get; set; } 

    [MaxLength(255)] 
    public string ProductName { get; set; } 

    public float Price { get; set; } 

    [MaxLength(255)] 
    public string Brand { get; set; } 

    public string category { get; set; } 

    public string ImageUrl { get; set; } 

    public string description { get; set; } 

    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 



public class Retailer 
{ 
    [PrimaryKey, AutoIncrement] 
    public int RetailerID { get; set; } 

    public string RetailerName { get; set; } 

    public string RetailerAddress { get; set; } 

    public string LogoUrl { get; set; } 
} 

答えて

1

あなたはsqlite-net-extensionsを使用することができるかもしれない、それはあなたのニーズにぴったりのようだ多対多の属性を持っています。

public class Product 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [PrimaryKey, AutoIncrement] 
    public int ProductID { get; set; } 

    [MaxLength(255)] 
    public string ProductName { get; set; } 

    public float Price { get; set; } 

    [MaxLength(255)] 
    public string Brand { get; set; } 

    public string category { get; set; } 

    public string ImageUrl { get; set; } 

    public string description { get; set; } 

    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    // You should add ProductsRetailers class 
    [ManyToMany(typeof(ProductsRetailers))] 
    public List<Retailer> Retailers { get; set; } 

} 



public class Retailer 
{ 
    [PrimaryKey, AutoIncrement] 
    public int RetailerID { get; set; } 

    public string RetailerName { get; set; } 

    public string RetailerAddress { get; set; } 

    public string LogoUrl { get; set; } 

    // You should add ProductsRetailers class 
    [ManyToMany(typeof(ProductsRetailers))] 
    public List<Product> Products { get; set; } 
} 
はまた、あなたがこの

public class ProductsRetailers 
{ 
    [ForeignKey(typeof(Product))] 
    public int ProductID { get; set; } 

    [ForeignKey(typeof(Retailer))] 
    public int RetailerID { get; set; } 
} 
+0

おかげのようなクラスProductsRetailersを追加する必要があります!!!!!!!!!!!!!!!!!!!!!!! !!!! –