使用Generic Repository插入/更新多对多

时间:2016-09-26 00:12:38

标签: c# entity-framework generics repository code-first

我在Generic Repository中插入和更新有问题,在通用插入或更新中它没有问题,但是在很多很多关系中我在插入时遇到错误:

IEntityChangeTracker的多个实例无法引用实体对象。

更新

无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象。

我的代码是

接口

 public interface IGenericRepository<TEntity>:IDisposable
    {
 void Insert(TEntity entity);
 void Update(TEntity entity);
}

通用类

  public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
            {
                private ApplicationDbContext context=null;
                private DbSet<TEntity> dbSet=null;
                public GenericRepository()
                {
                    this.context = new ApplicationDbContext();
                    this.dbSet = context.Set<TEntity>();
                }
                public GenericRepository(ApplicationDbContext context)
                {
                    this.context = context;
                    this.dbSet = context.Set<TEntity>();
                }

                public virtual void Insert(TEntity entity)
                {
Error is here--->  this.context.Set<TEntity>().Add(entity);
                   // dbSet.Add(entity);
                    context.SaveChanges();
                }

                public virtual void Update(TEntity entity)
                {
Error is here--->   dbSet.Attach(entity);
                    context.Entry(entity).State = EntityState.Modified;
                    context.SaveChanges();
                }

        }

和控制代码

        private IGenericRepository<Blog> _Repository = null;
        private IGenericRepository<BlogTag> _RepositoryTag = null;
        private IGenericRepository<BlogCategory> _RepositoryCategory = null;

        public BlogsController()
        {

            this._Repository = new GenericRepository<Blog>(new DbContext());
            this._RepositoryTag = new GenericRepository<BlogTag>(new DbContext());
            this._RepositoryCategory = new GenericRepository<BlogCategory>(new DbContext());
        }

     public async Task<ActionResult> Create([Bind(Include = "BlogID,BlogTitle,BlogContent,VisitCount,Preview")] Blog blog
                ,string[] SelectedTags,string[] SelectedCategories, HttpPostedFileBase files)
            {

                if (SelectedTags != null)
                {
                    blog.BlogTags = new List<BlogTag>();
                    foreach (var tag in SelectedTags)
                    {
                        var tagToAdd = _RepositoryTag.GetById(int.Parse(tag));
                        blog.BlogTags.Add(tagToAdd);
                    }
                }
                if (SelectedCategories != null)
                {
                    blog.BlogCategories = new List<BlogCategory>();
                    foreach (var cat in SelectedCategories)
                    {
                        var catToAdd = _RepositoryCategory.GetById(int.Parse(cat));
                        blog.BlogCategories.Add(catToAdd);
                    }
                }

                if (ModelState.IsValid)
                {
                    blog.DateTimeInsert = DateTime.UtcNow;
                    blog.DateTimeModify = DateTime.UtcNow;
                    blog.ImagePath= files != null ? Path.GetFileName(files.FileName) : "";
                    blog.BlogContent = HttpUtility.HtmlEncode(blog.BlogContent);

                    _Repository.Insert(blog);

                    return RedirectToAction("Index");
                }

                ViewBag.BlogTags = new SelectList(_RepositoryTag.Get(), "BlogTagID", "TagName");
                ViewBag.BlogCategories = new SelectList(_RepositoryCategory.Get(), "BlogCategoryID", "CategoriesName");

                return View(blog);
            }


            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Edit([Bind(Include = "BlogID,BlogTitle,BlogContent,VisitCount,Preview")] Blog blog
               , string[] SelectedTags, string[] SelectedCategories, HttpPostedFileBase files)
            {

                if (Request["BlogID"] == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                int id = int.Parse(Request["BlogID"].ToString());
                var blogsToUpdate =  _Repository.Query(i => i.BlogID == id, null).Include(t => t.BlogTags).Include(t => t.BlogCategories).Single();

                if (TryUpdateModel(blogsToUpdate, "",
                   new string[] { "BlogID", "BlogTitle", "BlogContent", "VisitCount","Preview" }))
                {
                    try
                    {


                        UpdateInstructorCourses(SelectedTags, SelectedCategories, blogsToUpdate);

                        blogsToUpdate.DateTimeModify = DateTime.UtcNow;
                        blogsToUpdate.DateTimeInsert = DateTime.UtcNow;
                        blogsToUpdate.BlogContent = HttpUtility.HtmlEncode(blogsToUpdate.BlogContent);

                        await _Repository.UpdateAsync(blogsToUpdate,  d => d.BlogTitle, d => d.VisitCount, d => d.BlogContent, d => d.ImagePath);

                        return RedirectToAction("Index");
                    }
                    catch (RetryLimitExceededException /* dex */)
                    {
                        //Log the error (uncomment dex variable name and add a line here to write a log.
                        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                    }
                }
                AssignedDDLCHKBoxValues(blogsToUpdate);
                return View(blogsToUpdate);
            }

            private void UpdateInstructorCourses(string[] SelectedTags, string[] SelectedCategories, Blog blogsToUpdate)
            {
                if (SelectedTags == null)
                {
                    blogsToUpdate.BlogTags = new List<BlogTag>();
                    return;
                }
                if (SelectedCategories == null)
                {
                    blogsToUpdate.BlogCategories = new List<BlogCategory>();
                    return;
                }

                var SelectedTagsHS = new HashSet<string>(SelectedTags);
                var SelectedCategoriesHS = new HashSet<string>(SelectedCategories);
                var blogTags = new HashSet<int>(blogsToUpdate.BlogTags.Select(c => c.BlogTagID));

                foreach (var tag in _RepositoryTag.Get())
                {
                    if (SelectedTagsHS.Contains(tag.BlogTagID.ToString()))
                    {
                        if (!blogTags.Contains(tag.BlogTagID))
                        {
                            blogsToUpdate.BlogTags.Add(tag);
                        }
                    }//if
                    else
                    {
                        if (blogTags.Contains(tag.BlogTagID))
                        {
                            blogsToUpdate.BlogTags.Remove(tag);
                        }
                    }//else
                }//foreach tag


                var blogcategories = new HashSet<int>
                   (blogsToUpdate.BlogCategories.Select(c => c.BlogCategoryID));
                foreach (var Category in _RepositoryCategory.Get())
                {
                    if (SelectedCategoriesHS.Contains(Category.BlogCategoryID.ToString()))
                    {
                        if (!blogcategories.Contains(Category.BlogCategoryID))
                        {
                            blogsToUpdate.BlogCategories.Add(Category);
                        }
                    }//if
                    else
                    {
                        if (blogcategories.Contains(Category.BlogCategoryID))
                        {
                            blogsToUpdate.BlogCategories.Remove(Category);
                        }
                    }//else
                }//foreach skill
            }

1 个答案:

答案 0 :(得分:0)

您的问题是您使用多个上下文来处理单个实体。 在您的控制器构造函数上,您有以下这些行:

this._Repository = new GenericRepository<Blog>(new DbContext());
this._RepositoryTag = new GenericRepository<BlogTag>(new DbContext());
this._RepositoryCategory = new GenericRepository<BlogCategory>(new DbContext());

在这里,您将创建3个应该一起工作的存储库9,具有3种不同的上下文。

之后,继续阅读RepositoryTag存储库,这里:

var tagToAdd = _RepositoryTag.GetById(int.Parse(tag));

执行此操作时,对象tagToAdd将附加到RepositoryTag内的上下文。如果您调试列表BlogTags,在其中添加此tagToAdd,您将看到您拥有动态代理,这意味着该对象已附加到上下文。

之后,您可以使用另一个上下文来填充存储库类别:

var catToAdd = _RepositoryCategory.GetById(int.Parse(cat));
blog.BlogCategories.Add(catToAdd);

现在,您的blog对象引用了2个不同的上下文:用于加载标记的上下文(RepositoryTag),以及用于加载博客类别的上下文({{1} })。

最后,您尝试将RepositoryCategory usgin用于第三个上下文:

blog

这将引发异常,因为EF无法使用这样的多个上下文。

要解决此问题,只需在存储库之前实例化上下文,并将其传递给所有yout存储库,如下所示:

_Repository.Insert(blog);

现在,不要忘记你应该处理你的情境。这就是为什么最推荐和最常用的方法是使用这样的代码:

this.context = new DbContext(); // The context you need to use for all operations you are performing here.
this._Repository = new GenericRepository<Blog>(this.context);
this._RepositoryTag = new GenericRepository<BlogTag>(this.context);
this._RepositoryCategory = new GenericRepository<BlogCategory>(this.context);