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