带有扩展方法的stackoverflow异常

时间:2012-09-27 09:40:29

标签: .net entity-framework .net-4.0 entity-framework-5

如何从DbContext中添加扩展方法?

这会引发堆栈溢出异常:

    public IQueryable<T> All<T>() where T : class, new()
    {
        return this.All<T>();
    }

我使用代码来调用它:

var u = UnitOfWork.All<User>().ToList();

工作单位是:

    protected DBContext UnitOfWork = new DBContext ();

这也引发了:

return this.All<T>().AsQueryable();

我的班级:

public class DBContext : DbContext, ISession
{

    public DBContext ()
        : this("name=myConnString")
    {

    }

    public SurventrixContext(string dbcontext)
        : base(dbcontext)
    {

    }

    #region mappings

    private void UserMappings(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().ToTable("tUsers");

        modelBuilder.Entity<User>().Property(x => x.UserID).HasColumnName("fU_UserID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<User>().Property(x => x.FirstName).HasColumnName("fU_First_Name");
        modelBuilder.Entity<User>().Property(x => x.LastName).HasColumnName("fU_Last_Name");
        modelBuilder.Entity<User>().Property(x => x.Login).HasColumnName("fU_Login");
        modelBuilder.Entity<User>().Property(x => x.Password).HasColumnName("fU_Password");
        modelBuilder.Entity<User>().Property(x => x.Email).HasColumnName("fU_Email");
        modelBuilder.Entity<User>().Property(x => x.Tel).HasColumnName("fU_Tel");
        modelBuilder.Entity<User>().Property(x => x.CreateDate).HasColumnName("fU_CreateDate");
        modelBuilder.Entity<User>().Property(x => x.LastPasswordChangedDate).HasColumnName("fU_LastPasswordChangedDate");
        modelBuilder.Entity<User>().Property(x => x.PasswordKey).HasColumnName("fU_PasswordKey");
        modelBuilder.Entity<User>().Property(x => x.KeyExpiryDate).HasColumnName("fU_KeyExpiryDate");
        modelBuilder.Entity<User>().Property(x => x.ApiToken).HasColumnName("fU_ApiToken");

        modelBuilder.Entity<User>().HasKey(t => t.UserID)
            .Property(x => x.UserID);
    }

    #endregion

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        UserMappings(modelBuilder);

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<User> User { get; set; }

    public DbSet<ReportCommit> ReportCommit { get; set; }

    public DbSet<Organization> Organization { get; set; }

    public void CommitChanges()
    {
        this.SaveChanges();
    }

    public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {
        this.Delete<T>(expression);
    }

    public void Delete<T>(T item) where T : class, new()
    {
        this.Delete<T>(item);
    }

    public void DeleteAll<T>() where T : class, new()
    {
        this.DeleteAll<T>();
    }

    public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T : class, new()
    {
        return this.Single<T>(expression);
    }

    public IQueryable<T> All<T>() where T : class, new()
    {
        return this.All<T>().AsQueryable();
    }

    public void Add<T>(T item) where T : class, new()
    {
        this.Add<T>(item);
    }

    public void Add<T>(IEnumerable<T> items) where T : class, new()
    {
        foreach (var item in items)
        {
            this.Add<T>(item);
        }
    }

    public void Update<T>(T item) where T : class, new()
    {
        this.Update<T>(item);
    }
}

1 个答案:

答案 0 :(得分:2)

这不是扩展方法!您刚刚创建了一个在递归中调用自身的方法。

扩展方法如下:

public static IQueryable<T> All<T>(this IQueryable<T> query) where T : class, new()
{
    return query.All();
}

编辑:

我检查了你的代码,你很可能根本就没有寻找扩展方法。您正在寻找:

public IQuerybale<T> All<T>() where T : class, new() 
{
    return this.Set<T>();
}