指定的模式无效。错误:由于“User”类型不可用,因此未加载“Product_CreatedByUser”关系 [英] Schema specified is not valid. Errors: The relationship 'Product_CreatedByUser' was not loaded because the type 'User' is not available
问题描述
指定的模式无效。错误:Models.Product_CreatedByUser关系未加载,因为User类型不可用。
我正在使用代码第一种方法,任何人都可以帮助我。
用户:
public class User
{
public long UserId {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public virtual ICollection< Product>产品{get;组;
}
产品:
public class Product
{
public long ProductId {get;组; }
public string ProductCode {get;组; }
public string Name {get;组; }
public DateTime CreatedDate {get;组; }
public long CreatedBy {get;组; }
public long ModifiedBy {get;组; }
public DateTime ModifiedDate {get;组; }
public bool IsDeleted {get;组; }
[ForeignKey(CreatedBy)]
public virtual User CreatedByUser {get;组; }
[ForeignKey(ModifiedBy)]
public virtual User ModifiedByUser {get;组; }
}
根据模型, 产品
与有2个多对一FK关系用户
- 1至 CreatedBy
和1到 ModifiedBy
。但是, User
类包含一个 Products
集合,而EF需要一个或零个相应的导航属性每个关系。此外,它不知道当前FK应该映射到哪个集合,所以它默默地尝试创建第三个关系:(由于默认映射不适用于这种情况,您需要使用 InverseProperty
或流畅的API来正确指定映射。
例如,双向映射可以这样指定:
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;}
}
Upate:虽然上述修正了映射问题,但它引入了另一个称为多个级联路径的问题,因为通过默认的EF约定级联删除打开一对多的关系。由于您需要流畅的配置,无论如何为一个或两个关系关闭级联删除,最好删除 ForeignKey
和 InverseProperty
注释,并配置整个流畅的东西:
modelBuilder.Entity< Product>()
.HasRequired(e = eCreatedByUser)
.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);
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.
User:
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; }
}
Product:
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; }
}
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; }
}
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);
这篇关于指定的模式无效。错误:由于“User”类型不可用,因此未加载“Product_CreatedByUser”关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!