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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.