【问题标题】:Code First Migration Creating an addition attributeCode First 迁移创建附加属性
【发布时间】: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


    【解决方案1】:

    这正是我在迁移中所期望的。您的 HistoryRead 中有一个虚拟的 AspNetUser,EF 创建了一个 AspNetUser_Id 以将其与您的用户匹配。它不知道您想使用 User_Id 作为键。这就是引发异常的原因。

    它在 book 上起作用的原因是因为它使用了 _Id 的约定并且它知道它是 book virtual 的外键。

    我建议您从模型中删除 User_Id 和 Book_Id。 EF 将处理它需要的关系和外键。这样模型会更干净,您可以通过虚拟访问它。

    如题,如何向对象添加新关系的示例。

    var historyObject = dbContext.Set<HistoryRead>().GetById(1);
    var bookObject = dbContext.Set<Book>().GetByTitle("Example");
    historyObject.Book = bookObject;
    dbContext.Set<HistoryRead>().Update(historyObject);
    await dbContext.SaveChangesAsync();
    

    【讨论】:

    • 我已经在 1 周前创建了另一个表并持有相同的属性和关系,但是当我使用迁移时它创建它时没有任何额外的属性!
    • 嗯,这很奇怪。我会做一些研究然后回来。
    • 如果我想遵循您的解决方案并删除 user_id 和 book_id ...如何将新数据添加到表中?因为如果我使用通常的方式,它将无法识别属性: HistoryRead ReadH = new HistoryRead() { Book_Id = result.Book_id, User_Id = x.ToString() };
    • 您将它们作为对象添加到 historyRead 对象(使用虚拟),然后保存历史读取对象。
    • 我不知道您如何使用您的 dbContext,但会举一个我通常如何使用的示例。希望你可以使用它。
    猜你喜欢
    • 2013-04-20
    • 2011-12-20
    • 2016-08-04
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多