【问题标题】:Adding an entity to a database entity framework generates an exception 'System.Data.Entity.Infrastructure.DbUpdateException将实体添加到数据库实体框架会生成异常 'System.Data.Entity.Infrastructure.DbUpdateException
【发布时间】:2017-07-31 23:04:48
【问题描述】:

我有一个由六个表(人、地址、电子邮件、电话、教育、出生、图片)组成的数据库。表角色与许多(地址,电子邮件,电话,教育)和一对一(出生,图片)有关系 表教育是一对一的关系,地址或一对多(电子邮件,电话) . 数据库是实体框架通过代码先创建的,没有porblemu创建空的。添加实体运行没有问题,直到您不包含任何类型的数据,教育,然后抛出异常:

EntityFramework.dll 中出现“System.Data.Entity.Infrastructure.DbUpdateException”类型的未处理异常

附加信息:更新条目时出错。有关详细信息,请参阅内部异常。

我的课程代码

[Table ("Person")]
public class Person
{
    [Key]
    [Column ("Person_ID")]
    public int PersonID { get; set; }
    [Column ("Name")]
    [Required]
    [MaxLength (50)]
    public string Name { get; set; }
    [Column ("Surname")]
    [Required]
    [MaxLength (50)]
    public string Surname { get; set; }
    [Timestamp]
    public byte[] RovVersion { get; set; }


    public virtual IList<Adress> Adress { get; set; }
    public virtual IList<Email> Mail { get; set; }
    public virtual IList<Phone> Phone { get; set; }
    public virtual IList<Education> Education { get; set; }
    public virtual Birth Birth { get; set; }
    public virtual Pictures Pictures { get; set; }
}
[Table ("Education")]
public class Education
{
    [Key]
    [Column("Education_ID")]
    public int EducationID { get; set; }
    [Column("Name")]
    [MaxLength(50)]
    public string NameOfScool { get; set; }
    [Column("Start")]
    public DateTime StartEducaton { get; set; }
    [Column("End")]
    public DateTime EndEducation { get; set; }
    [ForeignKey("Person")]
    [Column("Person_ID")]
    public int PersonID { get; set; }
    [ForeignKey("Adress")]
    [Column("Adress_ID")]
    public int AdresID { get; set; }

    public virtual Person Person { get; set; }
    public virtual Adress Adress { get; set; }
    public virtual IList<Email> Mail { get; set; }
    public virtual IList<Phone> Phone { get; set; }
}
[Table("Adress")]
public class Adress
{
    [Key]
    [Column ("Adress_ID")]
    public int AdressID { get; set; }
    [Column ("Adress_1")]
    [MaxLength (50)]
    public string Adress1 { get; set; }
    [Column ("Adress_2")]
    [MaxLength (50)]
    public string Adress2 { get; set; }
    [Column ("Number")]
    [MaxLength (10)]
    public string Number { get; set; }
    [Column ("Post")]
    [MaxLength (50)]
    public string Post { get; set; }
    [Column ("Code_Post")]
    [MaxLength (6)]
    public string CodePost { get; set; }
    [Column ("Region")]
    public EnumRegion Region { get; set; }
    [Column ("Country")]
    [MaxLength (50)]
    public string Country { get; set; }

    public virtual Person Person { get; set; }
}
[Table ("Birth")]
public class Birth
{
    [ForeignKey ("Person")]
    [Column ("Birth_ID")]
    public int BirthID { get; set; }
    [Column("Place_Of_Birth")]
    [MaxLength (50)]
    public string Place { get; set; }
    [Column ("Date_Of_Birth")]
    public DateTime Date { get; set; }

    public virtual Person Person { get; set; }
}
[Table("Phone")]
public class Phone
{
    [Key]
    [Column("Phone_ID")]
    public int PhoneId { get; set; }
    [Column("Phone_Number")]
    public int PhoneNumber { get; set; }
    [Column("Choise_Phone")]
    public EnumChoise Choise { get; set; }

    public virtual Person Person { get; set; }
    public virtual Education Education { get; set; }
}
[Table ("Email")]  
public class Email
{
    [Key]
    [Column("Adress_ID")]
    public int EmailId { get; set; }
    [Column("Mail")]
    [MaxLength(50)]
    public string Mail { get; set; }
    [Column("Choise_Mail")]
    public EnumChoise Choise { get; set; }

    public virtual Person Person { get; set; }
    public virtual Education Education { get; set; }
}

上下文类

public class EFContext :DbContext
{
    public EFContext()
        :base ("name=EntityModel")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<EFContext,
    Migrations.Configuration>());

    }
    public DbSet<Person> Person { get; set; }
    public DbSet<Adress> Adress { get; set; }
    public DbSet<Email> Mail { get; set; }
    public DbSet<Phone> Phone { get; set; }
    public DbSet<Birth> Birth { get; set; }
    public DbSet<Education> Education { get; set; }
    public DbSet<Pictures> Pictures { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().Property(e => e.RovVersion).IsRowVersion();
        base.OnModelCreating(modelBuilder);
    }

不知道是什么原因,请帮忙

【问题讨论】:

  • DbUpdateException 的内部异常是什么?您用来添加实体的代码是什么样的?

标签: c# entity-framework entity code-first


【解决方案1】:

正如错误消息所述,您需要查看内部异常以获取更多信息。

快速查看您的代码,我猜我可能会抱怨“出生”表上缺少主键

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2021-08-14
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多