不要在Entity Framework中映射ReactiveUI属性

Kry*_*oxx 1 c# entity-framework reactiveui

使用实体框架代码首先,我创建了一些对象来存储数据库中的数据.我在这些对象中的ReactiveUI库中实现了ReactiveObject类,因此每当prorerty更改为响应更快的UI时,我都会收到通知.

但实现它会为我的对象添加3个属性,即Changed,Changing和ThrowExceptions.我真的不认为这是一个问题,但是当在DataGrid中加载表时,这些都会得到一个列.

有没有办法隐藏这些属性?我不能只手动定义列,因为我的所有表都有1个数据网格,我从组合框中选择.

下面找到的解决方案也在这里:当AutoGenerateColumns = True时,有没有办法隐藏DataGrid中的特定列?

    void dataTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        List<string> removeColumns = new List<string>()
        {
            "Changing",
            "Changed",
            "ThrownExceptions"
        };

        if (removeColumns.Contains(e.Column.Header.ToString()))
        {
            e.Cancel = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Sim*_*ger 5

Code First有几种方法可以做到这一点.第一个选项是使用以下内容注释属性NotMappedAttribute:

[NotMapped]
public bool Changed { get; set; }
Run Code Online (Sandbox Code Playgroud)

现在,这是为了您的信息.因为您继承了基类并且无权访问该类的属性,所以不能使用它.第二个选择是使用流利的配置Ignore方法:

modelBuilder.Entity<YourEntity>().Ignore(e => e.Changed);
modelBuilder.Entity<YourEntity>().Ignore(e => e.Changing);
modelBuilder.Entity<YourEntity>().Ignore(e => e.ThrowExceptions);
Run Code Online (Sandbox Code Playgroud)

要访问DbModelBuilder,请覆盖以下OnModelCreating方法DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // .. Your model configuration here
}
Run Code Online (Sandbox Code Playgroud)

另一种选择是创建一个继承的类EntityTypeConfiguration<T>:

public abstract class ReactiveObjectConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity : ReactiveObject
{

    protected ReactiveObjectConfiguration()
    {
        Ignore(e => e.Changed);
        Ignore(e => e.Changing);
        Ignore(e => e.ThrowExceptions);
    }
}

public class YourEntityConfiguration : ReactiveObjectConfiguration<YourEntity>
{
    public YourEntityConfiguration()
    {
        // Your extra configurations
    }
}
Run Code Online (Sandbox Code Playgroud)

此方法的优点是您可以为所有人定义基线配置ReactiveObject并删除所有定义冗余.

有关上述链接中Fluent配置的更多信息.