一对多实体框架错误

时间:2015-03-12 02:35:35

标签: entity-framework mapping one-to-many code-first asp.net-web-api2

我有2个型号,用户&电话。用户可以拥有多个电话号码。

我的用户代码:

    public class User
    {
    public User()
     {
        this.TelephoneNumbers = new List<Telephone>();
     }
    public int UserId { get; set; }


    public string Forename { get; set; }


    public string Surname { get; set; }


    public string FirstLine { get; set; }


    public string Town { get; set; }


    public string County { get; set; }


    public string Postcode { get; set; }


    public string Email { get; set; }


    public int TravelDistance { get; set; }
    //[Display (Name = "Full Name")]
    public string FullName
    {
        get { return Forename + " " + Surname; }
    }

    //List to store telephone numbers for the users
    public virtual ICollection<Telephone> TelephoneNumbers { get; set; }

}

我的电话代码:

    public class Telephone
    {
    public int TelephoneId { get; set; }

    public string TelephoneNumber { get; set; }


    //Used to get UserId from User table
    public int UserId { get; set; }
    public virtual User User { get; set; }

   }

用户映射:

    public class UserMap : EntityTypeConfiguration<User>
    {
    public UserMap()
    {
        //Table Mapping
        this.ToTable("User");

        //Primary Key
        this.HasKey(u => u.UserId);

        //Properties
        this.Property(u => u.UserId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(u => u.Forename)
            .IsRequired();

        this.Property(u => u.Surname)
            .IsRequired();
    }
}

TelephoneMap

     public class TelephoneMap : EntityTypeConfiguration<Telephone>
     {

    public TelephoneMap()
    {
        //Table Mapping
        this.ToTable("Telephone");

        //Primary Key
        this.HasKey(t => t.TelephoneId);

        //Properties
        this.Property(t => t.TelephoneId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        this.Property(t => t.TelephoneNumber)
            .IsRequired();
       //Relationship
       //FK_Telephone_User
       //One to many relationship between user & telephone
       //One User has an iCollection of Telephone numbers
        this.HasRequired(t => t.User)
            .WithMany(t => t.TelephoneNumbers)
            .HasForeignKey(t => t.UserId);
    }

}

我已经运行了Add-Migration init,然后运行了Update-Database。

我收到以下错误:

System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Telephone_dbo.User_UserId". The conflict occurred in database "API_Context-20150311183614", table "dbo.User", column 'UserId'

我认为这与我的电话模型中的UserId有关?当我为数据库播种时,我不需要将它们连接在一起吗?

1 个答案:

答案 0 :(得分:0)

是的,你这样做。电话号码通过外键链接到用户。您需要通过指定用户的ID来指定其电话号码。您的约束被破坏的事实是由以下可能原因之一引起的:

  • 不可为空的外键(电话始终与用户相关联,不应悬浮在空中)
  • 外键可能具有默认值,该值在用户表
  • 中不存在

无论如何,如果我们谈论电话号码和用户,对我来说似乎难以置信,以防止错误,因为我没有看到任何使用没有链接到用户的电话号码,但是,我不太了解因此,关于你的项目,我的直觉可能是错误的。因此,您可以通过使外键可以为空来解决问题,但这似乎是错误的方法。相反,我认为您的代码应始终指定有效的电话号码。