小编Chr*_*ris的帖子

使用实体框架优化时间属性

我想使用类似于此处描述的方法实现时态属性,使用Entity Framework代码优先进行数据库存储.

我希望它针对获取当前值进行了优化,并且对历史记录进行了延迟加载,但是我不希望在父实体中为每次使用添加样板代码,就像上面链接中的方法一样.

目前我有类似下面的代码,按照惯例导致数据库模式如下所示代码.

这将按我的需要运行,但出于性能原因,我想避免获取当前属性值所需的连接(即我想将TemporalStrings.CurrentValue DB列移动到Entities.Name).

如果我试试

modelBuilder.Entity<Entity>().Property(o => o.Name.CurrentValue).HasColumnName("Name");
Run Code Online (Sandbox Code Playgroud)

它不起作用.我得到一个例外

The type 'ConsoleApplication1.TemporalString' has already been configured as an entity type. It cannot be reconfigured as a complex type.
Run Code Online (Sandbox Code Playgroud)

有什么方法可以实现这种映射,还是有更好的方法来实现这个功能?

码:

public class TemporalString
{
    public int Id { get; set; }
    public string CurrentValue { get; set; } // Setter would be customized to append to History.
    public virtual List<TemporalStringValue> History { get; set; }
    // Other methods such as string ValueAt(DateTime) would exist.
}

public …
Run Code Online (Sandbox Code Playgroud)

entity-framework temporal

9
推荐指数
0
解决办法
1231
查看次数

标签 统计

entity-framework ×1

temporal ×1