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

╄→尐↘猪︶ㄣ 提交于 2019-12-03 05:23:10

The error is a little cryptic, so I'm not sure if this is the reason you're getting that particular error, but I do know it will cause some error, so you can start by fixing this:

What you have is two one-to-many relationships to the same model on one class. That's not a problem per se, but you have to treat them as separate. In other words, they can't both have a opposite relationship of Orders, because relationally, there's no way to know which foreign key relationship should populate that list. If you simply change your fluent API definition to something like .WithMany(t => t.Orders_Shipping) and .WithMany(t => t.Orders_Billing), I think that will clear up your error.

Stacked

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

OrderAddress:

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

Fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Order>()
                .HasRequired(m => m.ShippingAddress)
                .WithMany(t => t.ShippingOrders)
                .HasForeignKey(m => m.ShippingAddressId)
                .WillCascadeOnDelete(false);

    modelBuilder.Entity<Order>()
                .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.

Had this issue. I believe it is something to do with the web service. My solution was after updating the service reference and the config operation took place. the problem went away. hope this helps.

In my case I had this error because I made another partial class with the same name as the class generated by entity framework. Except mine did not matche the case. For example:

public partial class Vehicle
{
    public string Name { get; set; }
    public string Make { get; set; }
    ...
}

public partial class VEhicle
{
    public override bool Equals(object obj)
    {
       ...
    }
}

I got the same error using database first: "The relationship was not loaded because the type ... is not available". The problem was the model in the solution was outdated. To fix the problem:

  • Double click on edmx file under Solution.
  • Right click on it.
  • Select "Update Model from Database...".
  • Click on the "Refresh" tab.
  • Click on the Finish button.

Your edmx now should be updated with the latest database changes.

This is common in self relation entities. in this case:

1-check to have ICollection, List, ... relation collection for each relation.

2-check name of relation collections.

3-in code first, check your EntityTypeConfiguration

I got one solution here! if you are using EF then do following,

Open model.edmx then right click on it and update model from database. finally build application and run it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!