【发布时间】:2012-12-18 12:14:26
【问题描述】:
我正在尝试在 .NET 4.5 上为 EF5 实现存储库/工作单元模式,如下所述:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application。这一切都很好,直到我尝试使用此处描述的自引用实体:http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code。当我尝试对包含自引用实体 (FieldType) 的存储库执行任何操作(获取指定实体、获取所有实体、添加实体等)时,我在 UnitOfWork 类中的 FieldTypeRepository 的 getter 上收到 stackoverflowexception。别担心,在 stackoverflow 上发布 stackoverflowexception 的讽刺意味对我来说并没有丢失。我已经阅读了很多关于 EF 存储库模式的 SO 文章,但没有看到任何将它们与自引用实体相结合的东西。
我尝试过的事情:
- 在 FieldType 实体上使用 FluentAPI 而不是属性 - 没有变化
- 直接使用 Context 对象 - 这很有效,实体被添加到数据库并正确地相互引用,不幸的是这违背了存储库模式的目的
- 为非自引用实体使用存储库 - 效果很好
- 将 EF5 用于 .NET 4.0 和 .NET 4.5 - 结果相同
我的代码:
FieldType.cs
public class FieldType
{
[Key]
[DatabaseGeneratedAttribute(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int FieldTypeId { get; private set; }
public string Name { get; set; }
public string Description { get; set; }
public int? ParentFieldTypeId { get; set; }
[ForeignKey("ParentFieldTypeId")]
public virtual FieldType ParentFieldType { get; set; }
}
UnitOfWork.cs
public class UnitOfWork : IDisposable
{
private Context context = new Context();
private Repository<FieldType> fieldTypeRepository;
public Repository<FieldType> FieldTypeRepository
{
// EXCEPTION OCCURS HERE
get
{
if (this.fieldTypeRepository == null)
{
this.fieldTypeRepository = new Repository<FieldType>(this.context);
}
return this.FieldTypeRepository;
}
}
public void Save()
{
this.context.SaveChanges();
}
}
上下文.cs
public class Context : DbContext
{
public Context()
: base("echo")
{
}
public DbSet<FieldType> FieldTypes { get; set; }
}
Repository.cs
public class Repository<TEntity> where TEntity : class
{
private Context context;
private DbSet<TEntity> dbSet;
public Repository(Context context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includedProperties = "")
{
IQueryable<TEntity> query = this.dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includedProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetById(object id)
{
return this.dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
this.dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = this.dbSet.Find(id);
this.Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (this.context.Entry(entityToDelete).State == EntityState.Detached)
{
this.dbSet.Attach(entityToDelete);
}
this.dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
this.dbSet.Attach(entityToUpdate);
this.context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
SecurityController.cs(或任何类)
public class SecurityController : Controller
{
public ActionResult Login()
{
using (UnitOfWork unitOfWork = new UnitOfWork())
{
var fieldType = unitOfWork.FieldTypeRepository.GetById(1);
//THIS COULD BE ANY TYPE OPERATION
}
return this.View();
}
【问题讨论】:
-
您可以添加堆栈跟踪的有意义的部分吗?
-
Gert - 不幸的是,堆栈跟踪只是 Core.dll!Core.Database.UnitOfWork.FieldTypeRepository.get() 的列表,直到超过 VS 支持的最大堆栈帧数:(
-
天啊,你才这么说,我看你应该这样做
return this.fieldTypeRepository;! (小写 f) -
哇...是的,做到了。感谢您的帮助!
-
查看ef4templates.codeplex.com 以生成存储库模式。
标签: c# asp.net-mvc-4 repository-pattern entity-framework-5 self-referencing-table