【问题标题】:One to Many entity framework error一对多实体框架错误
【发布时间】:2015-05-14 02:09:10
【问题描述】:

我有 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();
    }
}

电话地图

     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 有关吗?当我为数据库做种时,我不需要这个来将两者连接在一起吗?

【问题讨论】:

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


    【解决方案1】:

    是的,你知道。电话号码通过外键链接到用户。您需要通过指定用户的 id 来指定它的电话号码。您的约束被打破的事实是由以下可能的原因之一引起的:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 2020-09-26
      相关资源
      最近更新 更多