Schema specified is not valid. Errors: The relationship was not loaded because the type is not available

后端 未结 8 1452
旧时难觅i
旧时难觅i 2021-02-05 02:53

I wish to reference the OrderAddress model twice in my Order model; once as a ShippingAddress and once as a Billing

8条回答
  •  不要未来只要你来
    2021-02-05 03:08

    You need to change your OrderAddress entity and your Fluent API mappings to the following:

    OrderAddress:

    public class OrderAddress : BaseModel
    {
        ...
        public virtual ICollection BillingOrders { get; set; }
        public virtual ICollection ShippingOrders { get; set; }
        ...
    }
    

    Fluent API:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Entity()
                    .HasRequired(m => m.ShippingAddress)
                    .WithMany(t => t.ShippingOrders)
                    .HasForeignKey(m => m.ShippingAddressId)
                    .WillCascadeOnDelete(false);
    
        modelBuilder.Entity()
                    .HasRequired(m => m.BillingAddress)
                    .WithMany(t => t.BillingOrders)
                    .HasForeignKey(m => m.BillingAddressId)
                    .WillCascadeOnDelete(false);
    }
    

    Check this SO post for more, it is about the same problem as yours.

提交回复
热议问题