从一端定义关系有效,但从另一端定义它会导致错误
Defining a relationship from one end works, but defining it from the other end causes an error
为什么在OnModelCreating中调用base后的第一行会出错?抛出的注释行导致此错误:
表达式 'x => value(Blog.Services.Db).ContentItems' 不是有效的 属性 表达式。该表达式应表示 属性 访问:'t => t.MyProperty'。
参数名称:属性AccessExpression
protected override void OnModelCreating(ModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<ContentGroup>().HasMany(x => ContentItems).WithOne(x => x.ContentGroup).HasForeignKey(x => x.ContentGroupID); // same effect as following line but throws an error
//mb.Entity<ContentItem>().HasOne(x => x.ContentGroup).WithMany(x => x.ContentItems).HasForeignKey(x => x.ContentGroupID); // same effect as previous line but works
}
内容组:
public class ContentGroup
{
public int ID { get; set; }
public int SiteID { get; set; }
public string Description { get; set; }
public int Sequence { get; set; }
public bool Active { get; set; }
public virtual Site Site { get; set; }
public virtual ICollection<ContentItem> ContentItems { get; set; }
}
内容项:
public class ContentItem
{
public int ID { get; set; }
public string ContentType { get; set; }
public int ContentGroupID { get; set; }
public DateTime? PublishDate { get; set; }
public DateTime? LastModifyDate { get; set; }
public string ChangeFrequency { get; set; }
public decimal? Priority { get; set; }
public bool Active { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string MenuText { get; set; }
public string URI { get; set; }
public string Abstract { get; set; }
public string Icon { get; set; }
public string Tags { get; set; }
public bool AllowComments { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ContentGroup ContentGroup { get; set; }
public virtual ICollection<MenuContentItem> MenuContentItems { get; set; }
}
在第一个语句中,HasForeignKey()
方法反映到 ContentGroup
实体而不是 ContentItem
实体。 ContentGroup
实体没有 ContentGroupId
属性 这就是它不起作用的原因。
更新:
The expression 'x => value(Blog.Services.Db).ContentItems' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'. Parameter name: propertyAccessExpression
根据出现的异常消息,因为在第一个语句中你有 .HasMany(x => ContentItems)
但它应该是 .HasMany(x => x.ContentItems)
.
为什么在OnModelCreating中调用base后的第一行会出错?抛出的注释行导致此错误:
表达式 'x => value(Blog.Services.Db).ContentItems' 不是有效的 属性 表达式。该表达式应表示 属性 访问:'t => t.MyProperty'。 参数名称:属性AccessExpression
protected override void OnModelCreating(ModelBuilder mb)
{
base.OnModelCreating(mb);
mb.Entity<ContentGroup>().HasMany(x => ContentItems).WithOne(x => x.ContentGroup).HasForeignKey(x => x.ContentGroupID); // same effect as following line but throws an error
//mb.Entity<ContentItem>().HasOne(x => x.ContentGroup).WithMany(x => x.ContentItems).HasForeignKey(x => x.ContentGroupID); // same effect as previous line but works
}
内容组:
public class ContentGroup
{
public int ID { get; set; }
public int SiteID { get; set; }
public string Description { get; set; }
public int Sequence { get; set; }
public bool Active { get; set; }
public virtual Site Site { get; set; }
public virtual ICollection<ContentItem> ContentItems { get; set; }
}
内容项:
public class ContentItem
{
public int ID { get; set; }
public string ContentType { get; set; }
public int ContentGroupID { get; set; }
public DateTime? PublishDate { get; set; }
public DateTime? LastModifyDate { get; set; }
public string ChangeFrequency { get; set; }
public decimal? Priority { get; set; }
public bool Active { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string MenuText { get; set; }
public string URI { get; set; }
public string Abstract { get; set; }
public string Icon { get; set; }
public string Tags { get; set; }
public bool AllowComments { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ContentGroup ContentGroup { get; set; }
public virtual ICollection<MenuContentItem> MenuContentItems { get; set; }
}
在第一个语句中,HasForeignKey()
方法反映到 ContentGroup
实体而不是 ContentItem
实体。 ContentGroup
实体没有 ContentGroupId
属性 这就是它不起作用的原因。
更新:
The expression 'x => value(Blog.Services.Db).ContentItems' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'. Parameter name: propertyAccessExpression
根据出现的异常消息,因为在第一个语句中你有 .HasMany(x => ContentItems)
但它应该是 .HasMany(x => x.ContentItems)
.