3

Entity Framework コードの使用 最初に、データベースにデータを格納するためのオブジェクトをいくつか作成しました。これらのオブジェクトに ReactiveUI ライブラリの ReactiveObject クラスを実装しているため、UI の応答性が向上するようにプロパティが変更されるたびに通知を受け取ります。

しかし、これを実装すると、Changed、Changing、ThrowExceptions という 3 つのプロパティがオブジェクトに追加されます。これが問題だとは思いませんが、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;
        }
    }
4

1 に答える 1

6

Code First でこれを行う方法はいくつかあります。最初のオプションは、プロパティに次の注釈を付けることNotMappedAttributeです。

[NotMapped]
public bool Changed { get; set; }

さて、これはあなたの情報です。基本クラスを継承していて、そのクラスのプロパティにアクセスできないため、これを使用できません。2 番目のオプションは、次の方法でFluent Configurationを使用することです。Ignore

modelBuilder.Entity<YourEntity>().Ignore(e => e.Changed);
modelBuilder.Entity<YourEntity>().Ignore(e => e.Changing);
modelBuilder.Entity<YourEntity>().Ignore(e => e.ThrowExceptions);

にアクセスするには、次のメソッドをDbModelBuilderオーバーライドします。OnModelCreatingDbContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // .. Your model configuration here
}

別のオプションは、継承するクラスを作成すること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
    }
}

この方法の利点は、すべてのベースライン構成を定義しReactiveObject、すべての定義の冗長性を取り除くことです。

Fluent Configuration の詳細については、上記のリンクを参照してください。

于 2013-07-15T10:46:51.640 に答える