我試圖在.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屬性 - 無變化
- 直接使用上下文對象 - 這個完美的作品,實體被添加到數據庫,並參考各其他適當的,不幸的失敗存儲庫模式
- 使用存儲庫對非自參考實體的目的 - 適用於.NET 4.0和.NET 4.5大
- 使用EF5 - 同樣的結果
我的代碼:
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();
}
}
Context.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 - 不幸的是堆棧跟蹤只是一個\t Core.dll!Core.Database.UnitOfWork.FieldTypeRepository.get()的列表,直到超過了VS支持的最大堆棧幀數爲止:( – neuhoffm
OMG,只有現在你說這個我看到你應該做'return this.fieldTypeRepository'!'(小寫字母f) –