Добрый день!
Есть 2 таблицы people и countries
public people()
{
this.countries = new HashSet<countries>();
}
public long id { get; set; }
public string guid { get; set; }
public string full_name { get; set; }
public System.DateTime date_birth { get; set; }
public string birthplace { get; set; }
public string birthplace_fias { get; set; }
public string sex { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<countries> countries { get; set; }
}
public partial class countries
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public countries()
{
this.people = new HashSet<people>();
}
public int id { get; set; }
public string full_name { get; set; }
public string name { get; set; }
public string full_name_eng { get; set; }
public string name_eng { get; set; }
public string iso_num { get; set; }
public string iso2 { get; set; }
public string iso3 { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<people> people { get; set; }
}
При добавлении нового человека, EF пытается создать новую запись так же и в таблице countries, вместо того, что бы просто добавить запись в таблицу связей. Почему?
ICollection<countries> countries = new Collection<countries>();
countries.Add(new countries()
{
id = 182,
full_name = "Россия",
name = "Россия",
full_name_eng = "Russian Federation",
name_eng = "Russia",
iso_num = "643",
iso2 = "RU",
iso3 = "RUS"
});
peoplepdto = new people()
{
birthplace = "Уфа",
date_birth = DateTime.Today,
full_name = "Иванов Иван Иванович",
guid = Guid.NewGuid().ToString(),
sex = "М",
countries = countries
};