[英]Using EntityFramework C# code-first from database - how to map table with no primary key
[英]How map two properties to the same table using mvc 5 and EntityFramework code first?
我上這堂課。
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; }
}
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; }
}
請用上面的代碼編輯一下代碼。 希望這可以幫助..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.