【发布时间】:2016-04-29 16:33:43
【问题描述】:
我正在尝试使用具有这些属性的代码优先 EF 创建一个表:
public class HistoryRead
{
[Key]
public int Id { get; set; }
[Required]
public int Book_Id { get; set; }
[Required]
[StringLength(128)]
public string User_Id { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public virtual Book Book { get; set; }
}
我在两个表中都添加了所需的关系:
public partial class AspNetUser
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetUser()
{
HistoryReads = new HashSet<HistoryRead>();
}
public string Id { get; set; }
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
[StringLength(256)]
public string Email { get; set; }
public virtual ICollection<HistoryRead> HistoryReads { get; set; }
}
和:
public partial class Book
{
public Book()
{
HistoryReads = new HashSet<HistoryRead>();
}
[Key]
public int Book_id { get; set; }
[Required]
[Display(Name ="User Name")]
[StringLength(128)]
public string User_ID { get; set; }
public string UrlSlug { get; set; }
[Required]
[Display(Name = "Title Name")]
[StringLength(70,MinimumLength =3)]
public string Book_name { get; set; }
public virtual ICollection<HistoryRead> HistoryReads { get; set; }
}
然后我运行迁移并得到了这个结果!
public override void Up()
{
CreateTable(
"dbo.HistoryReads",
c => new
{
Id = c.Int(nullable: false, identity: true),
Book_Id = c.Int(nullable: false),
User_Id = c.String(nullable: false, maxLength: 128),
AspNetUser_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.AspNetUser_Id)
.ForeignKey("dbo.Book", t => t.Book_Id, cascadeDelete: true)
.Index(t => t.Book_Id)
.Index(t => t.AspNetUser_Id);
}
正如您在迁移生成的代码中看到的那样,我得到了一个名为“AspNetUser_Id”的额外属性,我不需要它,即使我试图删除它并继续我的工作,我也从 db 端得到了一个异常......所以如何解决这个问题,谢谢...
【问题讨论】:
标签: asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-migrations