0

My problem is that if I want to create two tables with a [OneToMany] relationship between these two.

public class Order : BusinessEntity
{
    [PrimaryKey]
    public Guid Id{ get; set; }

    [MaxLength(20)]
    public string Title{ get; set;}

    [MaxLength(20)]
    public string Location{ get; set; }


    public DateTime OrderGenerated{ get; set; }

    [OneToMany(CascadeOperations=CascadeOperation.All)]
    public List<OrderPos> Positions{ get; set; }
}

[Table("OrderPos")]
public class OrderPos : BusinessEntity
{
    [PrimaryKey]
    public Guid Id{ get; set; }

    [ForeignKey(typeof(Order)), ManyToOne]
    public Guid OrderId{ get; set; }

    [MaxLength(20)]
    public string MaterialNr{ get; set; }

    [MaxLength(10)]
    public string State{ get; set; }

    public int Amount{ get; set; }

}

The table OrderPos was created without the foreignkey and so I get an exception at the InsertOrUpdateAllWithChildren method.

Should I use another derived class of SqliteConnection?

Best Regards, Thomas

redent84
  • 18,901
  • 4
  • 62
  • 85

1 Answers1

1

You don't need the ManyToOne attribute in the foreign ket, you would only use this if you want to load the inverse relationship, i.e. the Order object.

Replace this:

[ForeignKey(typeof(Order)), ManyToOne]
public Guid OrderId{ get; set; }

With this:

[ForeignKey(typeof(Order))]
public Guid OrderId{ get; set; }

Also I don't think Sqlite supports Guids (or it will treat as text and performance will be bad), try using int and AutoIncrement.

Otherwise it looks fine.

Ref: https://bitbucket.org/twincoders/sqlite-net-extensions

redent84
  • 18,901
  • 4
  • 62
  • 85
alanh
  • 1,153
  • 10
  • 14