LI WEIJIE
LI WEIJIE

Reputation: 33

SQLite-Net Extension many-to-many relationships between two entities

I am using SQLite-Net PCL together with SQLite-Net extensions for the development of an application using Xamarin.

In my model I have two entities, let us call them product and seller, that are connected by many-to-many relationships. For instance, product can be sold by many sellers, and each seller selling many products.

Do I need to create another model to build this relationship? ProductId is product's primary key and also is sellers' foreign key.

Here is my models below:

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; }
}

Upvotes: 0

Views: 478

Answers (1)

Wilson Vargas
Wilson Vargas

Reputation: 2899

You might be able to use sqlite-net-extensions, it has a ManyToMany attribute that seems perfect for your needs.

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; } 
}

Additionally you must add the class ProductsRetailers like this

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

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

Upvotes: 1

Related Questions