实体对象不能由IEntityChangeTracker的多个实例中的通用信息库所引用 [英] Entity object cannot be referenced by multiple instances of IEntityChangeTracker in a generic repository
问题描述
我使用了一个通用接口和存储库在这个<一个href=\"http://www.tugberkugurlu.com/archive/generic-repository-pattern-entity-framework-asp-net-mvc-and-unit-testing-triangle\"相对=nofollow> tugberkugurlu ,这是...
i have used a generic interface and repository in this tugberkugurlu, which is...
public interface IGenericRepository<T> where T : class {
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
void Save();
}
和这个通用库
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class where C : DbContext, new() {
private C _entities = new C();
public C Context {
get { return _entities; }
set { _entities = value; }
}
public virtual IQueryable<T> GetAll() {
IQueryable<T> query = _entities.Set<T>();
return query;
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate) {
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
public virtual void Add(T entity) {
_entities.Set<T>().Add(entity);
}
public virtual void Delete(T entity) {
_entities.Set<T>().Remove(entity);
}
public virtual void Edit(T entity) {
_entities.Entry(entity).State = System.Data.EntityState.Modified;
}
public virtual void Save() {
_entities.SaveChanges();
}
}
这现在可以像这样使用...
which can now be used like this...
public class FooRepository :
GenericRepository<FooBarEntities, Foo>, IFooRepository {
public Foo GetSingle(int fooId) {
var query = GetAll().FirstOrDefault(x => x.FooId == fooId);
return query;
}
}
现在,一切,或者是很好,直到我有一个自参考实体,像这样...
now, all is, or was well until i had a self referencing entity, like this...
public class Question
{
[Key]
public string QuestionID { get; set; }
public string QuestionNumber { get; set; }
public string Message { get; set; }
public DateTime? DatePosted { get; set; }
public DateTime? Modified { get; set; }
public bool HasSubgroups { get; set; }
public string ApplicationUserId { get; set; }
[ForeignKey("ApplicationUserId")]
public virtual ApplicationUser ApplicationUser { get; set; }
public string PaperID { get; set; }
[ForeignKey("PaperID")]
public virtual PaperEntity Paper { get; set; }
public ICollection<QuestionTag> Tags { get; set; }
public ICollection<QuestionVote> Votes { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
public virtual ICollection<QuestionComment> QuestionComments { get; set; }
public string ParentQuestionID { get; set; }
[ForeignKey("ParentQuestionID")]
public virtual Question QuestionReference { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
和,你可以看到,我的模型让我有子问题的问题。这就是我如何努力实现它......
and as you can see, my model allows me to have sub-questions to a question. and this is how i try to implement it...
dynamic model = modela;
string q_id = model.QuestionID.ToString();
var question = q_id.IsNullOrEmpty() ? null : await questionRepository.FindBy(id => id.QuestionID == q_id);
if (question == null)
{
question = new Question
{
QuestionID = Guid.NewGuid().ToString("D"),
QuestionNumber = model.ParentQuestionNumber,
PaperID = model.PaperID,
Message = model.QuestionTitle,
ApplicationUserId = userid,
DatePosted = DateTime.UtcNow,
Tags = new List<QuestionTag>()
};
await questionRepository.Add(question);
}
var questionQuestion = new Question
{
QuestionID = Guid.NewGuid().ToString("D"),
ParentQuestionID = question.QuestionID,
QuestionNumber = model.QuestionNumber,
PaperID = question.PaperID,
Message = model.Message,
ApplicationUserId = userid,
DatePosted = DateTime.UtcNow,
Tags = new List<QuestionTag>()
};
question.Questions = new List<Question>{questionQuestion};
await questionRepository.Update(question);
await questionRepository.Save();
和我能够保存的第一个子问题之后,第二个问题在...抛出一个错误
and after i am able to save the first sub-question, the second question throws an error at...
_ctx.Entry(entity).State = EntityState.Modified;
说...
A first chance exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
我甚至使用的IDisposable
在 GenericRepository
没有运气尝试......任何暗示闻援助将是非常AP preciated ...
i have even tried using IDisposable
on GenericRepository
without luck...Any hint smelling assistance will be highly appreciated...
推荐答案
我想ü有因为环境的问题。大概u有两种情况使用相同的实体。这可能是因为创造了上下文每库
。尝试使用的UnitOfWork
格式,或者只是为所有仓库一个上下文。
I suppose u have problem because of contexts. Probably u have two contexts with the same entity. It can be because of creating context-per-repository
. Try to use UnitOfWork
pattern, or just create one context for all repositories.
这篇关于实体对象不能由IEntityChangeTracker的多个实例中的通用信息库所引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!