我正在使用SQLite-Net PCL和SQLite-Net扩展来开发使用Xamarin的应用程序。
在我的模型中,我有两个实体,我们称之为产品和卖方,它们通过多对多关系连接起来。例如,产品可以由许多卖家出售,每个卖家销售许多产品。
我是否需要创建另一个模型来建立这种关系? 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; }
}
答案 0 :(得分:1)
您可以使用sqlite-net-extensions,它具有看起来非常适合您需求的ManyToMany属性。
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; }
}
此外,您必须添加类似ProductsRetailers的类
public class ProductsRetailers
{
[ForeignKey(typeof(Product))]
public int ProductID { get; set; }
[ForeignKey(typeof(Retailer))]
public int RetailerID { get; set; }
}