我上这堂课。
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public virtual ICollection<Post> PostsCreated { get; set; }
public virtual ICollection<Post> PostsModified { get; set; }
public virtual Comment Comment { get; set; }
}
而另一类是...
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public virtual ICollection<Category> Category { get; set; }
public DateTime PublishDate { get; set; }
public DateTime CreateDate { get; set; }
//----------------------------------------------------------------
[ForeignKey("UserCreated"), Column(Order = 0)]
public string CreatedId { get; set; }
[ForeignKey("UserModify"), Column(Order = 1)]
public string ModifiedId { get; set; }
//----------------------------------------------------------------
[InverseProperty("PostsCreated")]
public virtual ICollection<ApplicationUser> UserCreated { get; set; }
[InverseProperty("PostsModified")]
public virtual ICollection<ApplicationUser> UserModify { get; set; }
//----------------------------------------------------------------
public DateTime ModifiedDate { get; set; }
public Public Public { get; set; }
}
我的数据访问权限是这个.....
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("BlogLibrary2")
{
}
public DbSet<Post> Post { get; set; }
public DbSet<Comment> Comment { get; set; }
public DbSet<Category> Category { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
当我尝试使用此数据访问和Post类创建控制器时,出现了此错误:
运行所选代码生成器时发生错误:“无法检索'LibraryBlog.Models.Post”的元数据。外键组件'CreatedId'不是类型为'ApplicationUser'的已声明属性。请验证未将其显式排除在模型之外,并且它是有效的原始属性。
我修复了该错误。我修改了此类,现在看起来像这样:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public virtual ICollection<Post> PostsCreated { get; set; }
public virtual ICollection<Post> PostsModified { get; set; }
public virtual Comment Comment { get; set; }
}
和这个:
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public virtual ICollection<Category> Category { get; set; }
public DateTime PublishDate { get; set; }
public DateTime CreateDate { get; set; }
//---------------------Here is the point
public string PostCreatedBy { get; set; }
public string PostModifiedBy { get; set; }
[ForeignKey("PostCreatedBy"), InverseProperty("PostsCreated")]
public virtual ApplicationUser CreatedBy { get; set; }
[ForeignKey("PostModifiedBy"), InverseProperty("PostsModified")]
public virtual ApplicationUser ModifiedBy { get; set; }
//-------------------------------------------
public DateTime ModifiedDate { get; set; }
public Public Public { get; set; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句