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