指定的模式无效。错误:没有装载的关系,因为该类型不可用 [英] Schema specified is not valid. Errors: The relationship was not loaded because the type is not available
问题描述
我想引用 OrderAddress
模式的两次我在订单
模型;一次作为 ShippingAddress
一次作为 BillingAdress
。
I wish to reference the OrderAddress
model twice in my Order
model; once as a ShippingAddress
and once as a BillingAdress
.
在另一边,我希望我的 OrderAddress
模式对名单 OrderAddresses
。
On the other side, I want my OrderAddress
model to have a list of OrderAddresses
.
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; }
}
订货型号
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; }
}
流利的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:
指定的模式是无效的。错误:关系MyApp.Domain.DAL.Order_ShippingAddress未加载,因为类型'MyApp.Domain.DAL.OrderAddress不可用
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.
我在做什么错了?
推荐答案
该错误是有点神秘,所以我不知道这是你得到的特定错误的原因,但我知道它会导致的部分的错误,这样你就可以解决这个启动:
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:
你有什么是两个一到多的关系,在一类相同的模型。这本身并不是一个问题,但你必须把它们当作独立的。换句话说,他们不能都具有订单的对立关系
,因为相关法,也没有办法知道哪些外键关系应该填充该列表。如果你简单地改变你的流利的API定义为类似于 .WithMany(T =&GT; t.Orders_Shipping)
和 .WithMany(T =&GT;吨.Orders_Billing)
,我认为这将清理你的错误。
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.
这篇关于指定的模式无效。错误:没有装载的关系,因为该类型不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!