M
M
Michael2016-06-01 09:52:37
C++ / C#
Michael, 2016-06-01 09:52:37

Entity Framework, Many to Many, why is an extra entry created?

Good afternoon!
There are 2 tables people and 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; }
    }

When adding a new person, EF tries to create a new entry in the countries table as well, instead of just adding an entry to the link table. Why?
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
            };

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rifat, 2016-06-01
@Slider_vm

I think here you should not create a countries object, but get it from the database

ICollection<countries> countries = _countriesRepository.Get(...);

people peoplepdto = new people()
            {
                birthplace = "Уфа",
                date_birth = DateTime.Today,
                full_name = "Иванов Иван Иванович",
                guid = Guid.NewGuid().ToString(),
                sex = "М",                           
                countries = countries
            };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question