我有这个班......
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'类型的声明属性。验证它尚未从模型中明确排除,并且它是一个有效的原始属性。'
答案 0 :(得分:2)
我修正了这个错误。我修改了这个类,现在看起来像这样:
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; }
}
答案 1 :(得分:0)
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("BlogLibrary2")
{
}
public DbSet<Post> posts { get; set; }
public DbSet<Comment> comments { get; set; }
public DbSet<Category> categories { get; set; }
}
请您使用上面的代码编辑代码。希望这会有所帮助..