Sourabh Dhiman

Sourabh Dhiman

  • NA
  • 42
  • 898

Save Same Id Multiple Time

Jun 15 2021 6:09 AM

I have use this code for get and set data using linq but i have face a issue same id not store multiple time 

foreach (var customEntity in customEntityResult.Items)
            {
                var model = (BlogPostDataModel)customEntity.Model;
                var blogPost = new BlogPostSummary();
                blogPost.Title = customEntity.Title;
                blogPost.ShortDescription = model.ShortDescription;
                DateTime tempDate = customEntity.CreateDate;
                blogPost.PostDate = tempDate.ToString("MMMM dd, yyyy");
                blogPost.FullPath = customEntity.PageUrls.FirstOrDefault();
                blogPost.CategoryId = model.CategoryIds.SingleOrDefault();
                blogPosts.Add(blogPost);

                Person obj_addBlogList = new Person();
                obj_addBlogList.Address = customEntity.Title;
                obj_addBlogList.FirstName = model.ShortDescription;
                obj_addBlogList.PersonID = model.CategoryIds.SingleOrDefault();--Here Id Multiple Time 
                _context.Persons.Add(obj_addBlogList);
                _context.SaveChanges();

Face This Issue

The instance of entity type 'Person' cannot be tracked because another instance with the same key value for {'PersonID'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.


Answers (1)