gpt4 book ai didi

c# - 不要在 Entity Framework 中映射 ReactiveUI 属性

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:39 25 4
gpt4 key购买 nike

使用 Entity Framework 代码 首先,我创建了一些对象来将数据存储在我的数据库中。我在这些对象中实现了 ReactiveUI 库中的 ReactiveObject 类,因此每当为响应速度更快的 UI 更改属性时,我都会收到通知。

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

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

解决方案在下方和此处找到:Is there a way to hide a specific column in a DataGrid when AutoGenerateColumns=True?

    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;
}
}

最佳答案

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

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

现在,这是供您引用的信息。因为您继承了一个基类并且无权访问该类的属性,所以您不能使用它。第二种选择是使用 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 , 覆盖 OnModelCreating DbContext 中的方法:

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 的更多信息,请参见上面的链接。

关于c# - 不要在 Entity Framework 中映射 ReactiveUI 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17652315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com