[英]How can I enforce camelCasing in cosmos/EF when my .NET classes are named with PascalCasing?
尝试从 cosmos 查询数据时遇到此错误:
实体类型“DataModel”的分区键设置为“partitionKey”,但没有具有该名称的属性。
这是我的数据模型 class 的样子:
public class DataModel
{
/* Properties - Serializable */
public string Id { get; set; } = string.Empty;
public int? Index { get; set; } = null;
public string? PartitionKey { get; set; } = null;
public string Name { get; set; } = string.Empty;
public bool Locked { get; set; } = false;
public string? LockedUser { get; set; } = null;
public List<Record> Records { get; set; } = new List<Record>();
public List<Field> Fields { get; set; } = new List<Field>();
/* Properties - Not-Serializable */
[JsonIgnore]
[NotMapped]
public Record? RootRecord = null;
[JsonIgnore]
[NotMapped]
public BehaviorSubject<Record?> RootRecordBH = new BehaviorSubject<Record?>(null);
}
下面是我如何实现 OnModelCreating:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DataModel>().Property(x => x.Id).ToJsonProperty("id");
modelBuilder.Entity<DataModel>().Property(x => x.Index).ToJsonProperty("index");
modelBuilder.Entity<DataModel>().Property(x => x.PartitionKey).ToJsonProperty("partitionKey");
modelBuilder.Entity<DataModel>().Property(x => x.Name).ToJsonProperty("name");
modelBuilder.Entity<DataModel>().Property(x => x.Locked).ToJsonProperty("locked");
modelBuilder.Entity<DataModel>().Property(x => x.LockedUser).ToJsonProperty("lockedUser");
modelBuilder.Entity<DataModel>().Property(x => x.Records).ToJsonProperty("records");
modelBuilder.Entity<DataModel>().Property(x => x.Fields).ToJsonProperty("fields");
modelBuilder.Entity<DataModel>()
.ToContainer("DataModels")
.HasPartitionKey("partitionKey");
}
我的理解是,在使用 EF/EFCore 时,您可以通过在 OnModelBuilding() 中使用 .ToJsonProperty 来控制 cosmos 中的属性名称,尽管这似乎什么都不做。 我不太确定我在这里错过了什么。
我尝试执行有问题的共享代码,并能够使用 camelCasing 格式将数据保存在 CosmosDB 中。 我的数据 model class 有 PascalCasing。
代码中所做的唯一更改是.HasPartitionKey(x=>x.PartitionKey);
正如Jeremy Lakeman在评论中提到的 HasPartitionKey 方法接受 model 属性名称而不是 json 名称。
还要执行我在 nuget 包及其各自版本下使用的代码。
参考:-
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.