0
votes

Tools: I am using Xamarin forms MVVM and SQLite-Net-pcl

Error cannot convert from 'System.Type' to 'string'

how to set up basic ForeignKey with different names for primarykey and foreignkey? I am getting an error on line

[ForeignKey(typeof(ProductModel))]

ProductModel class

*class ProductModel
{
    [PrimaryKey, AutoIncrement]
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductURL { get; set; }
}

ordermodel class

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Sqlite;

class OrderModel
{
    [PrimaryKey, AutoIncrement]
    public int OrderId { get; set; }
    
    [ForeignKey(typeof(ProductModel))]
    public int ProdcutId_FK { get; set; }
    
    public int OrderQty { get; set; }
}*
1
you need to use SQLIte.Net Extensions for FK support - bitbucket.org/twincoders/sqlite-net-extensions/src/master - Jason
is this a nuget i can install? - user9207271
if you read the docs it explains how to install it - Jason
just install it and not working... I think ill just move to different database. but thanks - user9207271

1 Answers

0
votes

Try this


class OrderModel
{
    [PrimaryKey, AutoIncrement]
    public int OrderId { get; set; }
    
    public int OrderQty { get; set; }
   
    public int ProductId { get; set; }

    [ForeignKey(nameof(ProductId ))]
    [InverseProperty("Orders")]
    public ProductModel Product { get; set; }
  
}

class ProductModel
{
    [PrimaryKey, AutoIncrement]
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductURL { get; set; }

    [InverseProperty(nameof(OrderModel.Product))]
    public virtual ICollection<OrderModel> Orders{ get; set; }
}