将实体添加到数据库实体框架会生成异常&System; System.Data.Entity.Infrastructure.DbUpdateException

时间:2017-03-10 14:48:08

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

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

未处理的类型' System.Data.Entity.Infrastructure.DbUpdateException'发生在EntityFramework.dll

其他信息:更新条目时发生错误。有关详细信息,请参阅内部异常。

我班级的代码

[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);
    }

我不知道造成这种情况的原因并请帮助

1 个答案:

答案 0 :(得分:0)

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

快速查看一下您的代码,我猜可能会抱怨说#34;诞生&#34;缺少主键。表