Duplicate rows in the table RRS feed

  • Question

  • Hello

     I am trying to follow an approach to organize the project in n-tier, but many-to-many n tier architecture not working.
     The problem is when add a new entity articles with existing articletypes it duplicates in the table
     Then describe my code:

    I'm using the following layers

    In Presentation Layer, I Have the controller

    private readonly IAppArticleService _articleAppService;
            private readonly IAppTypeArticleService _typeArticleAppService;

            public ArticleController(IAppArticleService articleAppService, IAppTypeArticleService typeArticleAppService)
                _articleAppService = articleAppService;
                _typeArticleAppService = typeArticleAppService;

    public ActionResult Create( ArticlesViewModel articleviewmodel, int[] Articletypes)

                var resultTypeArticle = (from p in _typeArticleAppService.GetAll() 
                              where Articletypes.Contains(p.TypeArticleId)
                              select p).ToList();

                var articletypearticle = Mapper.Map<ICollection <TypeArticle>,  ICollection <TypeArticleViewModel>>(resultTypeArticle);
                articleviewmodel.TypeArticles = articletypearticle;

                ViewBag.TypeArticles = articletypearticle;

                if (ModelState.IsValid)
                    var articledomain = Mapper.Map<ArticlesViewModel, Article>(articleviewmodel);

                    return RedirectToAction("Index");

                return View(articleviewmodel);

    // I use the automapper to map entities

    CreateMap<ArticlesViewModel, Article>();
            CreateMap <TypeArticleViewModel, TypeArticle>();
            CreateMap<ArticlesViewModel, Article>().ForMember(dest => dest.TypeArticles, opt => opt.MapFrom(src => src.TypeArticles));

    CreateMap<Article, ArticlesViewModel>();
    CreateMap<TypeArticle, TypeArticleViewModel>();
            CreateMap<Article, ArticlesViewModel>().ForMember(dest => dest.TypeArticles, opt => opt.MapFrom(src => src.TypeArticles));

    I use fluent api entity framework to make relationship between tables

    HasMany<TypeArticle>(s => s.TypeArticles)
                .Map(cs =>

                HasMany<Comment>(s => s.Comments)
                       .WithRequired(s => s.Article)
                       .HasForeignKey(s => s.ArticleId);

    context.TypeArticle.AddOrUpdate(x => x.TypeArticleId,
    new TypeArticle { Type = "Geral" },
    new TypeArticle { Type = "technician" },
    new TypeArticle { Type = "Other" });

    In layer domain 
    public class Article
            public int ArticleId { get; set; }

            public string Title { get; set; }

            public virtual ICollection<TypeArticle> TypeArticles { get; set; }


    public class TypeArticle

            public int TypeArticleId { get; set; }
            public string Type { get; set; }

            public virtual ICollection<Article> Articles{ get; set; }

    In layer use generic repository.

     public class RepositoryBase<TEntity> : IDisposable, IRepositoryBase<TEntity> where TEntity : class

            protected ArticleDBContext Db = new ArticleDBContext();

            public void Add (TEntity obj)



    Before DB.set The obj have Article with 2 typeArticle {TypeArticleId=2, TypeArticleId=3}]
    After Db.SaveChanges typeArticle  have a new Id for example{TypeArticleId=6, TypeArticleId=7}]
    How to solve this problem.
    Thursday, May 4, 2017 8:37 AM

All replies

  • Hi rmp1980,

    >>Before DB.set The obj have Article with 2 typeArticle {TypeArticleId=2, TypeArticleId=3}]After Db.SaveChanges typeArticle  have a new Id for example{TypeArticleId=6, TypeArticleId=7}]

    Could you describe it in detailed, do you mean that you want to modify the typeArticle's Type instead of change the TypeArticleId. if so, could you please provide some related code about this changing.

    In addition, about entity framework many-to-many updating, please refer to:

    Best regards,

    Cole Wu

    MSDN Community Support&lt;br/&gt; Please remember to click &amp;quot;Mark as Answer&amp;quot; the responses that resolved your issue, and to click &amp;quot;Unmark as Answer&amp;quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact &lt;a href=&quot;;&gt;;/a&gt;.

    Friday, May 5, 2017 3:02 AM

    Thanks for the answer, I'll try to explain what my problem is.

    In table TypeArticle, the Seed create the following records.
    TypeArticleId Type
    1 Geral
    2 All
    3 Opinion

    After execute method Create in RepositoryBase  

        public void Add (TEntity obj)



    TEntity obj is Entities.Article
      In Db.Set<TEntity>().Add(obj); //the obj is following
       ArticleId =0
       Description "Test"
       Title Test "Test"
       TypeArticles Count=2   [0]{ArticlesDomain.Entities.TypeArticle}
       Articles count 1
    Type "All"
    TypeArticleId 2
       Articles count 1
    Type "Opinion"
    TypeArticleId 3

     Db.SaveChanges(); // After DB.SaveChanges()
    In table TypeArticle have two new records 
    1 Geral
    2 All
    3 Opinion
    4 All
    5 Opinion
    Friday, May 5, 2017 1:29 PM
  • Can you post your EF model and a short, but complete repro of the issue?


    Microsoft Technology Center - Dallas
    My blog

    Friday, May 5, 2017 3:16 PM