У меня есть 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; }
}
UserMap:
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, а затем обновил базу данных.
Я получаю следующее сообщение об ошибке:
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 в моей модели телефона? Мне не нужно это, чтобы соединить эти два вместе, когда я посева базы данных?