2

使用带有 POCO 的 EntityFramework 4.3。如何检查模型上的属性是否被忽略。

在我的 DBContext 类层次结构中,我忽略了一个属性

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

在我的 BaseContext 类中,我需要检查该属性是否被忽略。

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

我怎样才能做到这一点?

谢谢

4

1 回答 1

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; }
}
于 2013-12-08T19:20:32.190 回答