1

使用EntityFramework 4.3 w/POCOs。 如何檢查模型上的屬性是否被忽略?檢查某個屬性是否被EntityFramework忽略

在我的DbContext類層次結構,我通過

modelBuilder.Entity<EClass>().Ignore (f => f.IgnoredProperty()); 

忽略的屬性在我BaseContext類,我需要檢查如果屬性被忽略或沒有。

private void ProcessGlobalConvention(DbModelBuilder modelBuilder, IGlobalConvention convention) 
{ 
    modelBuilder.Entity<typeof(this.GetType())>("Ignored Property"); 
} 

我該怎麼做?

感謝

回答

0

使用EF電動工具http://www.infoq.com/news/2013/10/ef-power-tools-beta4來查看你的模型。那裏的財產?

創建數據庫。那裏是專欄嗎?

看那Database.LogSqlEvents http://blog.oneunicorn.com/2013/05/08/ef6-sql-logging-part-1-simple-logging/和分析SQL,看是否出現的字段名稱...

....除非你真的代碼解決方案...?

在這種情況下

新你的DbContext 創建一個記錄,並將其添加到相關DbSet 獲取DbEntityEntry 查找範圍CurrentValues.PropertyNames。你的財產在那裏嗎?

[TestMethod] 
    public void CreateDatabase() 
    { 
     Database.SetInitializer(new DropCreateDatabaseAlways<HomesContext>()); 

     var db = new HomesContext(); 

     Assert.IsFalse(db.Homes.Any()); 

     var home = db.Homes.Create(); 

     db.Homes.Add(home); 

     var entry = db.Entry(home); 

     Assert.IsTrue(entry.CurrentValues.PropertyNames.Contains("MaxResidents")); 
     Assert.IsTrue(entry.CurrentValues.PropertyNames.Contains("MaxStaff")); 
     Assert.IsFalse(entry.CurrentValues.PropertyNames.Contains("CurrentResidents")); 
     Assert.IsFalse(entry.CurrentValues.PropertyNames.Contains("CurrentStaff")); 

    } 
public class HomesContext:DbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Home>().Ignore(x => x.CurrentResidents); 

     base.OnModelCreating(modelBuilder); 
    } 
    public DbSet<Home> Homes { get; set; } 
} 

public class Home 
{ 
    public int HomeId { get; set; } 
    public string HomeName { get; set; } 

    public int MaxResidents { get; set; } 
    public int MaxStaff { get; set; } 

    public int CurrentResidents { get; set; } 

    [NotMapped] 
    public int CurrentStaff { get; set; } 
} 
+0

是的,我需要一個代碼解決方案。我可以找出[NotMapped]屬性,但是當您使用模型構建器忽略列時,我無法檢測到該屬性。 – hazimdikenli 2013-12-09 13:16:41