[英]Get Entity Type in Interception for EntityFramework
我在實體框架截取中遇到了一些問題。 在我的場景中,我想在選擇sql statemant由sql服務器執行之前對其進行修改。 使用攔截時可以看到命令,但是是否有一種簡單的方法來獲取為此腳本生成的實體類型? 我在攔截方法中找不到有關目標實體的任何信息。
我從主項目創建了一個非常簡單的改編。
實體:
[Table("Empty", Schema = "Base")]
public class EmptyBase
{
[Key]
[Column("ID", Order = 0)]
public Guid ID { get; set; }
}
[Table("SoftDele", Schema ="Base")]
public class SoftDeleteBase : EmptyBase
{
[Column("IsDeleted")]
public bool IsDeleted { get; set; }
[Column("IsActive")]
public bool IsActive { get; set; }
}
[Table("ToolTip", Schema = "Base")]
public class ToolTipBase : SoftDeleteBase
{
[Column("ToolTip")]
public string ToolTip { get; set; }
}
[Table("Test1", Schema = "Data")]
public class Test1 : ToolTipBase
{
[Column("Test1Content")]
public string Test1Content { get; set; }
}
[Table("Test2", Schema = "Data")]
public class Test2 : SoftDeleteBase
{
[Column("Test2Content")]
public string Test2Content { get; set; }
}
DbContext和攔截器:
public class DatabaseCTX : DbContext
{
public DatabaseCTX() : base(@"Data Source=localhost;Initial Catalog = SQL_Interception; Integrated Security = True")
{
//Database.SetInitializer(new DropCreateDatabaseAlways<DatabaseCTX>());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Models.EmptyBase> EmptyBases { get; set; }
public DbSet<Models.SoftDeleteBase> SoftDeleteBases { get; set; }
public DbSet<Models.Test1> Test1s { get; set; }
public DbSet<Models.Test2> Test2s { get; set; }
public DbSet<Models.ToolTipBase> ToolTipBases { get; set; }
}
public class EFInterception : DbConfiguration
{
public EFInterception()
{
this.AddInterceptor(new EFCommandInterceptor());
this.AddInterceptor(new EFCommandTreeInterceptor());
}
}
public class EFCommandTreeInterceptor : IDbCommandTreeInterceptor
{
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
//throw new NotImplementedException();
}
}
public class EFCommandInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
//throw new NotImplementedException();
}
public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
//throw new NotImplementedException();
}
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
//throw new NotImplementedException();
}
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
//throw new NotImplementedException();
}
public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
//throw new NotImplementedException();
}
public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
//throw new NotImplementedException();
}
}
在我的測試應用程序窗口中至少有一個小代碼:
public partial class MainWindow : Window
{
Controls.DatabaseCTX databaseCTX;
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
databaseCTX = new Controls.DatabaseCTX();
//MessageBox.Show(typeof(EF_Interception.Controls.)
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if(databaseCTX == null)
databaseCTX = new Controls.DatabaseCTX();
databaseCTX.Test1s.Add(new Models.Test1() { ID = Guid.NewGuid(), IsActive = true, Test1Content = "asgsewarasfd asdgasdfgasdf asdfg asdfds", ToolTip = "Nope" });
databaseCTX.Test2s.Add(new Models.Test2() { ID = Guid.NewGuid(), IsActive = true, Test2Content = "asgsewarasfd asdgasdfgasdf asdfg asdfds"});
databaseCTX.SaveChanges();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if (databaseCTX == null)
databaseCTX = new Controls.DatabaseCTX();
var a = databaseCTX.Test1s.ToList();
var b = databaseCTX.Test2s.ToList();
MessageBox.Show(a.Count.ToString() + "_" + b.Count.ToString());
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
if (databaseCTX == null)
databaseCTX = new Controls.DatabaseCTX();
var c = databaseCTX.SoftDeleteBases.ToList();
MessageBox.Show(c.Count.ToString());
}
問題在於Button_Click_3-方法中生成的選擇腳本還包含所有子表(test1,test2)中的所有字段。在這個小示例中,一切正常,但是當我在具有60-70個表的Big數據庫中使用這種類型的概念時,基表生成的Select腳本要在sql-server上執行很復雜。 在正常情況下,這是沒有問題的,因為我直接使用子實體(test1,test2),但是當我嘗試更改實體的“ IsDeleted”,“ IsActive”或“ ToolTip”屬性時出現問題。
如果要在保存dbcontext之前進行更改,則可以使用SaveChanges方法。
https://exceptionnotfound.net/entity-change-tracking-using-dbcontext-in-entity-framework-6/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.