MySQL EntitiyFramework Master-Detail Insert throws exception

Viewed 9

I'm trying to insert master-detail record on this way:

public partial class invoice
{          
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    public int Number { get; set; }   
}


public partial class invoiceitems
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
   
    public int IDInovice{ get;set; }
    public int IDItem { get; set; }

    [ForeignKey("IDInovice")]
    public virtual invoice invoice { get; set; }
    
}

Entering data and saving:

var db = new invoicing();

var inv = new invoice()
{
     ID = 888,
     Number = 12,
};
            
var invitems = new invoiceitems()
{
     invoice = inv,
     ID = 2,
     IDItem = 777
};

db.invoice.Add(inv);
db.invoiceitems.Add(invitems);
db.SaveChanges();

And here I'm receiving this kind of exception: What I'm missing?

System.Data.Entity.Core.MetadataException HResult=0x80131939
Message=Schema specified is not valid. Errors: The relationship 'invoicing.invoiceinvoiceitems' was not loaded because the type 'invoicing.invoice' is not available. The following information may be useful in resolving the previous error: The required property 'invoiceitems' does not exist on the type 'invoicing'.

0 Answers
Related