简体   繁体   English

指定的架构无效。 错误:未加载关系,因为类型不可用

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

I wish to reference the OrderAddress model twice in my Order model;我希望在我的Order模型中两次引用OrderAddress模型; once as a ShippingAddress and once as a BillingAdress .一次作为ShippingAddress ,一次作为BillingAdress

On the other side, I want my OrderAddress model to have a list of OrderAddresses .另一方面,我希望我的OrderAddress模型有一个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流利的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:当我尝试运行 Update-Database 时,出现以下错误:

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.错误:关系“MyApp.Domain.DAL.Order_ShippingAddress”未加载,因为类型“MyApp.Domain.DAL.OrderAddress”不可用。

What am I doing wrong?我究竟做错了什么?

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.换句话说,它们不能同时具有相反的Orders关系,因为从关系Orders ,无法知道哪个外键关系应该填充该列表。 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.如果您只是将流畅的 API 定义更改为.WithMany(t => t.Orders_Shipping).WithMany(t => t.Orders_Billing) ,我认为这将消除您的错误。

You need to change your OrderAddress entity and your Fluent API mappings to the following:您需要将OrderAddress实体和 Fluent API 映射更改为以下内容:

OrderAddress:订购地址:

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

Fluent API:流畅的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.查看此SO 帖子了解更多信息,它与您的问题大致相同。

This is common in self relation entities.这在自关系实体中很常见。 in this case:在这种情况下:

1-check to have ICollection, List, ... relation collection for each relation. 1-检查每个关系的ICollection、List、...关系集合。

2-check name of relation collections. 2-检查关系集合的名称。

3-in code first, check your EntityTypeConfiguration 3-in 代码先,检查你的 EntityTypeConfiguration

In my case, the problem is because of table name conflict .就我而言,问题是由于表名冲突

I have another edmx model that has some tables with similar names.我有另一个 edmx 模型,其中包含一些名称相似的表。 Just click on the table in the designer, then in the properties change the name to something different!只需单击设计器中的表格,然后在属性中将名称更改为不同的名称

I got one solution here!我在这里找到了一个解决方案! if you are using EF then do following,如果您使用的是 EF,请执行以下操作,

Open model.edmx then right click on it and update model from database.打开 model.edmx 然后右键单击它并从数据库更新模型。 finally build application and run it.最后构建应用程序并运行它。

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.双击解决方案下的 edmx 文件。
  • 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.您的 edmx 现在应该使用最新的数据库更改进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 指定的EF6模式无效。 错误:由于类型y不可用,所以未加载关系x - EF6 Schema specified is not valid. Errors: The relationship x was not loaded because the type y is not available 指定的 EntityFramework 架构无效。 错误: - EntityFramework Schema specified is not valid. Errors: 指定的架构无效。 错误:同名的多种类型 - Schema specified is not valid. Errors: Multiple types with the name 未加载FK关系,因为该类型不可用 - The FK relationship was not loaded because the type is not available 由于类型xx不可用,因此未加载关系xx - The relationship xx was not loaded because the type xx is not available 未加载关系,因为在托管网站时类型不可用错误 - The relationship was not loaded because the type is not available error while hosting website 由于以下异常,无法生成模型:&#39;指定的转换无效。 &#39; - Unable to generate the model because of the following exception: 'Specified cast is not valid. ' 指定的演员表无效。 是因为我试图投射一个对象吗? - Specified cast is not valid. Is it because I am attempting to cast an object? System.Data.Entity.Core.MetadataException:&#39;指定的架构无效。 - System.Data.Entity.Core.MetadataException: 'Schema specified is not valid. 指定的演员表无效。 林克 - specified cast is not valid. linq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM