简体   繁体   English

带有扩展方法的stackoverflow异常

[英]stackoverflow exception with extension methods

How can I add extension methods off the DbContext? 如何从DbContext中添加扩展方法?

This throws a stack overflow exception: 这会引发堆栈溢出异常:

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

I am calling this using the code: 我使用代码调用它:

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

Unit of work is: 工作单位是:

    protected DBContext UnitOfWork = new DBContext ();

This also throws: 这也引发:

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

My Class: 我的课:

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);
    }
}

That is not an extension method! 那不是扩展方法! You have just created a method which calls itself in recursion. 您刚刚创建了一个在递归中调用自身的方法。

Extension method looks like: 扩展方法如下:

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

Edit: 编辑:

I checked for your code and you are most probably not looking for extension methods at all. 我检查了你的代码,你很可能根本就没有寻找扩展方法。 You are looking for: 您正在寻找:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM