具有私有设置程序的EFCore基本实体模型属性-迁移异常行为 [英] EFCore Base entity model properties with private setters - migration bizarre behavior

查看:152
本文介绍了具有私有设置程序的EFCore基本实体模型属性-迁移异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎实体模型继承的类中的私有设置器会引起EFCore迁移的奇怪问题。考虑下面的示例,其中存在多个继承自的类( Bar Baz ) Foo 。多次运行 Add-Migration 命令(添加/删除 private 修饰符)时,生成的架构是错误的

It seems private setters inside classes that entity models inherit from cause bizarre issues with EFCore migrations. Consider the following example, where there are multiple classes (Bar and Baz) that inherit from Foo. When running Add-Migration commands multiple times (add/remove the private modifier`), the generated schema is just wrong in multiple ways.


  • 有时将 Created 属性设置为被删除的列

  • 有时 all Created 属性设置为被删除(而不是用任何东西替换/重命名) )。

  • Sometimes the Created property is set to be a dropped column
  • Sometimes all the the Created properties are set to be removed (and not replaced/renamed with anything).

据我了解,EFCore工具并没有真正特别重视带有私人资产的房地产经纪人。这是一个错误的假设吗?如果是这样,为什么一些私有设置方法在基类上起作用,而在其他类上不起作用?我假设这是EF工具中的错误,可能与它对待属性命名的方式有关,考虑到我在模型中还有其他名称相似的属性,这可能会混淆工具

It's my understanding that EFCore tools don't really treat properties with private setters with any special care. Is this a wrong assumption? If so, why do some private setters work on base classes, but not others? I'm assuming this is a bug in the EF Tools, perhaps related to how it treats naming on properties, considering I have other properties with similar names in the model which might be confusing the tools

public class Context : DbContext
{
    public DbSet<Bar> Bars { get; set; }
    public DbSet<Baz> Bazs { get; set; }
}

public class Bar : Foo { }
public class Baz : Foo { }
public abstract class Foo
{
    protected Foo()
    {
        Created = DateTimeOffset.UtcNow;
    }
    public DateTimeOffset? Created { get; private set; }
    public DateTimeOffset? Updated { get; set; }
}

编辑->似乎DateTimeOffset的私有设置方法导致EF Tools从不映射他们在基类。但是,我有一个字符串属性,带有一个带有 [必需] [StringLength] 属性的私有设置器,以及 builder.Entity< Foo>()。HasAlternateKey(x => x.RequiredStringProperty); 在这种情况下,EF会绝对映射该属性...但仅包含其中一些从 Foo 继承的类。

Edit -> It seems private setters for DateTimeOffset cause EF Tools to never map them in base classes. However, I have a string property with a private setter with [Required] and [StringLength] attributes, along with builder.Entity<Foo>().HasAlternateKey(x => x.RequiredStringProperty); In that case, EF absolutely maps the property...but only with some of the classes that are inheriting from Foo.

推荐答案

EF核心工具将

例如,如果您有 builder.Entity< Bar>()。HasAlternateKey(x => x.Created); ,则EF工具将映射 Created 属性,而不管它是私有设置者还是公共设置者。

For example, if you have builder.Entity<Bar>().HasAlternateKey(x => x.Created);, then EF tools will map the Created property regardless of whether it has a private or public setter.

这篇关于具有私有设置程序的EFCore基本实体模型属性-迁移异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆