31

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

On the other side, I want my OrderAddress model to have a list of OrderAddresses.

OrderAddress Model


public enum AddressType
{
    Billing,
    Shipping,
    Contact
}
public class OrderAddress : BaseModel
{
    public AddressType AddressType { get; set; }
    public bool IsPrimary { get; set; }

    public string Address { get; set; }
    public string CityStateZip { get; set; }
    public string ContactName { get; set; }
    public string PhoneNumber { get; set; }
    public string FaxNumber { get; set; }
    public string EmailAddress { get; set; }

    public virtual ICollection<Order> Orders { get; set; }

    public virtual ApplicationUser User { get; set; }
}

Order Model


public class Order : BaseModel
{
    public DateTime OrderDate { get; set; }

    public int BillingAddressId { get; set; }
    public virtual OrderAddress BillingAddress { get; set; }

    public int ShippingAddressId { get; set; }
    public virtual OrderAddress ShippingAddress { get; set; }

    public virtual ApplicationUser User { get; set; }

}

Fluent API


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

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

    modelBuilder.Entity<Order>()
                .HasRequired(m => m.BillingAddress)
                .WithMany(t => t.Orders)
                .HasForeignKey(m => m.BillingAddressId)
                .WillCascadeOnDelete(false);
}

When I try to run Update-Database, I get the following error:

Schema specified is not valid. Errors: The relationship 'MyApp.Domain.DAL.Order_ShippingAddress' was not loaded because the type 'MyApp.Domain.DAL.OrderAddress' is not available.

What am I doing wrong?

Fred Fickleberry III
  • 2,439
  • 4
  • 34
  • 50

8 Answers8

46

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.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • 2
    More detailed explanation http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table – JimSTAT May 17 '14 at 03:54
  • Chris' answer fixed the error described for me, however, if naming is important for inheritance (in a table per concrete type) or interface implementation, removing the fluent API and defining your relationships with [Key] and [ForeignKey("xxx")] attributes fixed the error and allowed the property names to remain the same (in my case the fluent API notation had been required in EF 4.1, but not more modern versions) – Brent Aug 04 '14 at 20:31
  • 2
    @Brent: Your comment is a little confusing. First, I went with Fluent configuration because that's what the OP was already using. You can just as well solve the problem with attribute-based configuration. However, there's no way to have both relationships share the same property name. If you configured it this way, it may "work" in the sense that you don't get errors but one relationship or the other will get exclusive access to that property, not both. – Chris Pratt Aug 05 '14 at 17:40
  • Logically, it makes sense. Sometimes when working with Code First we forget common sense. Thanks Chris! – Mike Devenney Apr 25 '17 at 13:47
6

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.

Community
  • 1
  • 1
Stacked
  • 6,892
  • 7
  • 57
  • 73
1

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.

live-love
  • 48,840
  • 22
  • 240
  • 204
1

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

Ali Mardan
  • 147
  • 5
1

In my case, the problem is because of table name conflict.

I have another edmx model that has some tables with similar names. Just click on the table in the designer, then in the properties change the name to something different!

ARZ
  • 2,461
  • 3
  • 34
  • 56
0

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.

0

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.

0

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)
    {
       ...
    }
}