如何使Add-Migration包括私有财产?

时间:2016-05-21 15:46:56

标签: c# entity-framework

现在我的模特看起来像这样:

public abstract class Base
{
    public Guid Id { get; set; }
    private byte[] RowVersion { get; set; }
}

public sealed class Derived : Base
{
    public string Name { get; set; }
}

运行Add-Migration时,一切都按预期工作,但没有创建RowVersion列。

无论如何要包括RowVersion列吗?

2 个答案:

答案 0 :(得分:0)

Usualy EF允许您通过流畅的api进行映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Entity()
        .Property(p => p.Property);
}

但您的私有财产不可见......

所以你必须创建一个EntityTypeConfiguration,然后注册它们:

public abstract class Base
{
    public Guid Id { get; set; }
    private byte[] RowVersion { get; set; }

    public class BaseConfiguration : EntityTypeConfiguration<Base>
    {
        public BaseConfiguration()
        {
            Property(p => p.RowVersion);
        }
    }

}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Configurations.Add(new Base.BaseConfiguration());
}

答案 1 :(得分:0)

您必须添加时间戳注释:

public abstract class Base
{
    public Guid Id { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }
}

public sealed class Derived : Base
{
    public string Name { get; set; }
}

为什么你的课程被密封了?!动态代理不起作用!小心那个。它必须是公开的(反思)。