0

如何从 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);
    }
}
4

1 回答 1

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>();
}
于 2012-09-27T09:45:53.807 回答