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