【问题标题】:EF Core 5 - Backing field: specified field could not be found for propertyEF Core 5 - 支持字段:找不到属性的指定字段
【发布时间】:2021-12-13 09:21:01
【问题描述】:

我正在尝试创建一个由 JSON 字符串组成的支持字段,然后由模型的属性访问该字符串并转换为 JObject:

public class StaticTable
    {
        public int StaticTableId { get; set; }
        private string _staticData { get; set; }

        public JObject StaticData
        {
            get => JsonConvert.DeserializeObject<JObject>(string.IsNullOrEmpty(_staticData) ? "{}" : _staticData);
            set => _staticData = value.ToString();
        }

        public static void RunFluent(ModelBuilder modelBuilder)
        {
            EntityTypeBuilder<StaticTable> entity = modelBuilder.Entity<StaticTable>();
            entity.Property(s => s.StaticData).HasField("_staticData");
        }
    }

创建迁移时,我收到以下错误: System.InvalidOperationException: The specified field '_staticData' could not be found for property 'StaticTable.StaticData'.

注意RunFluentDbContext.OnModelCreating中执行

【问题讨论】:

    标签: c# entity-framework .net-core


    【解决方案1】:

    尝试使用私有变量而不是属性

    private string _staticData;
    

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 2013-05-06
      • 2019-05-30
      • 2022-01-11
      相关资源
      最近更新 更多