entityframework alwayse adds two records in the tables RRS feed

  • Question

  • User-259252065 posted

    I'm implementing asp.net core 3.1. I have implemented following code to insert record in SQL server Database via ef core but each time I save data, it inserts two records in PersonRequester table and Requester table. I appreciate if anyone suggests me how i can prevent reinserting records.

     Requester ap = new Requester();
                        ap.Address = RequesterViewModel.Requestervm.Address;
                        ap.RequesterType = RequesterViewModel.Requestervm.RequesterType;
                        ap.Description = RequesterViewModel.Requestervm.Description;
                        ap.Name = RequesterViewModel.Requestervm.Name;
                            var pa = new PersonRequester()
                                BirthCertificateNo = RequesterViewModel.personRequestervm.BirthCertificateNo,
                                IssuePlace = RequesterViewModel.personRequestervm.IssuePlace,
                                NationalCode = RequesterViewModel.personRequestervm.NationalCode,
                                Requester = ap
                            using (var context = new DBContext())
                            catch (Exception e)
                                throw e;
        public partial class Requester
            public Requester()
                PersonRequester = new HashSet<PersonRequester>();
            public int RequesterId { get; set; }
            public int RequesterType { get; set; }
            public string Address { get; set; }
            public string Description { get; set; }
            public string Name { get; set; }
            public virtual EntityType RequesterTypeNavigation { get; set; }
            public virtual ICollection<PersonRequester> PersonRequester { get; set; }
       public partial class PersonRequester
            public int RequesterId { get; set; }
            public int RequesterType { get; set; }
            public string NationalCode { get; set; }
            public string BirthCertificateNo { get; set; }
            public string IssuePlace { get; set; }
            public virtual Requester Requester { get; set; }
            public virtual EntityType RequesterTypeNavigation { get; set; }

    Tuesday, March 16, 2021 12:07 PM

All replies

  • User-474980206 posted

    Most likely you are calling this code twice. Use debugging.

    Tuesday, March 16, 2021 2:52 PM
  • User-259252065 posted

    Thank you for your response, as you can see the code is what I provided earlier and there is no duplicate in my code. I also debugged my code but there is no any replication.

    Tuesday, March 16, 2021 4:34 PM
  • User1120430333 posted

    You should be using the Debugger Quickwatch to look at the content of the object to see if it has other object graphs in child collections that are going to be saved within the object you are trying to save. EF is just not going save additional objects to other tables, unless you have something going on inside the object that you are not aware of being in the object.

    Tuesday, March 16, 2021 7:59 PM
  • User303363814 posted

    how i can prevent reinserting

    Don't call the code which inserts twice.

    Maybe someone is double-clicking? You haven't shown how this code gets called so we can't tell you why it is called twice.

    It is very common to assume that one of the well tested, widely used frameworks is faulty ("entityframework always add two records") but it is far more likely that the programmer has made  a mistake.  A better title might be "Why am I running this code twice?"

    PS - Do not - ever, ever - catch an Exception and then throw it.  This just makes the stack trace point at the wrong line.  Delete the entire try/catch and you will have much superior code)

    Wednesday, March 17, 2021 12:10 AM