简体   繁体   中英

EF Core 5 - Backing field: specified field could not be found for property

I'm trying to create a backing field that consists of a JSON string which is then accessed by a property of the model and converted to a 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");
        }
    }

When creating a migration, I receive the following error: System.InvalidOperationException: The specified field '_staticData' could not be found for property 'StaticTable.StaticData'.

Note that RunFluent is executed in DbContext.OnModelCreating

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

private string _staticData;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM