指定的架构无效.错误:关系' Product_CreatedByUser'未加载,因为类型' User'不可用 [英] Schema specified is not valid. Errors: The relationship 'Product_CreatedByUser' was not loaded because the type 'User' is not available

查看:76
本文介绍了指定的架构无效.错误:关系' Product_CreatedByUser'未加载,因为类型' User'不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码优先模型,我尝试初始化数据库,并在下面提到错误.一个用户可以拥有多个产品,因此用户与产品之间存在一对多的关系.

I have following code first models and I try to initialized database it giving me error mention below. One user can have multiple products so there is one to many relationship between user and product.

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

我正在使用代码优先方法,任何人都可以帮我这个忙.

I am using code first approach, can any one help me with this.

用户:

public class User
{
    public long UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public virtual ICollection<Product> Products { get; set; }
} 

产品:

public class Product
    {
    public long ProductId { get; set; }
    public string ProductCode { get; set; }
    public string Name { get; set; }
    public DateTime CreatedDate { get; set; }
    public long CreatedBy { get; set; }
    public long ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public bool IsDeleted { get; set; }
    [ForeignKey("CreatedBy")]
    public virtual User CreatedByUser { get; set; }
    [ForeignKey("ModifiedBy")]
    public virtual User ModifiedByUser { get; set; }
}

推荐答案

根据模型, Product User 有2个多对一FK关系-1通过 CreatedBy 和1通过 ModifiedBy .但是, User 类包含单个 Products 集合,而EF对于每个关系都需要一个或零个对应的导航属性.同样,它也不知道当前FK应该映射到哪个FK,因此它默默地尝试创建第三个关系:(由于默认映射不适用于这种情况,因此您需要使用InverseProperty 或流利的API以正确指定映射.

According to the model, Product has 2 many-to-one FK relationships to User - 1 through CreatedBy and 1 through ModifiedBy. However, the User class contains a single Products collection, while EF requires one or zero corresponding navigation property for each relationship. Also, it doesn't know to which of the current FK is supposed to map that collection, so it silently tries to create a 3rd relation :( Since the default mapping doesn't work for such scenarios, you need to use InverseProperty or fluent API to specify the mappings correctly.

例如,可以这样指定双向映射:

For instance, the bidirectional mapping can be specified like this:

public class User
{
    public long UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [InverseProperty("CreatedByUser")]
    public virtual ICollection<Product> CreatedProducts { get; set; }
    [InverseProperty("ModifiedByUser")]
    public virtual ICollection<Product> ModifiedProducts { get; set; }
}

更新:尽管以上内容解决了映射问题,但它引入了另一个问题,称为多个级联路径,因为默认情况下,EF约定级联删除功能适用于以下情况:一对多的关系.由于无论如何都需要流利的配置才能关闭一个或两个关系的级联删除,因此最好删除 ForeignKey InverseProperty 批注,并流畅地配置整个过程:

Upate: While the above fixes the mapping issue, it introduces another issue known as multiple cascade paths, because by the default EF conventions cascade delete is on for one-to-many relationships. Since you need fluent configuration anyway to turn the cascade delete off for one or both relationships, you'd better remove ForeignKey and InverseProperty annotations and configure the whole thing fluently:

modelBuilder.Entity<Product>()
    .HasRequired(e => e.CreatedByUser)
    .WithMany(e => e.CreatedProducts)
    .HasForeignKey(e => e.CreatedBy)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<Product>()
    .HasRequired(e => e.ModifiedByUser)
    .WithMany(e => e.ModifiedProducts)
    .HasForeignKey(e => e.ModifiedBy)
    .WillCascadeOnDelete(false);

这篇关于指定的架构无效.错误:关系&amp;#39; Product_CreatedByUser&amp;#39;未加载,因为类型&amp;#39; User&amp;#39;不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆