我正在尝试在 .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 文章,但没有看到任何将它们与自引用实体结合起来的东西。
我尝试过的事情:
- 使用 FluentAPI 而不是 FieldType 实体上的属性 - 没有变化
- 直接使用 Context 对象 - 这非常有效,实体被添加到数据库并正确地相互引用,不幸的是这违背了存储库模式的目的
- 将存储库用于非自引用实体 - 效果很好
- 将 EF5 用于 .NET 4.0 和 .NET 4.5 - 结果相同
我的代码:
字段类型.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; }
}
存储库.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();
}