简体   繁体   English

Azure Cosmos DB(EF/Core)-驼峰式属性名称

[英]Azure Cosmos DB (EF/Core) - Camel Case Property Names

I have a .NET Core 3.1 API Project which has Cosmos DB storage being handled via Entity Framework (Microsoft.EntityFrameworkCore.Cosmos - v3.1.5).我有一个 .NET Core 3.1 API 项目,它通过实体框架(Microsoft.EntityFrameworkCore.Cosmos - v3.1.5)处理 Cosmos DB 存储。

I have a database model:我有一个数据库 model:

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class BikeRental
{
    [JsonProperty(PropertyName = "id")]
    [Key]
    public Guid Id { get; set; }

    [JsonProperty(PropertyName = "bikeId")]
    public string BikeId { get; set; }

    [JsonProperty(PropertyName = "shopId")]
    public string ShopId { get; set; }
}

Upon saving to the CosmosDB database, the columns are being serialised using the class property names, ignoring the 'PropertyName' attribute.保存到 CosmosDB 数据库后,将使用 class 属性名称对列进行序列化,忽略“PropertyName”属性。 For example, if 'bikeId' is changed to 'testBikeId' it is written as 'BikeId' still.例如,如果 'bikeId' 更改为 'testBikeId' 它仍然写为 'BikeId'。

{
    "Id": "192dfdf4-54cb-4290-a478-7035518983ca",
    "BikeId": "eb65b93b-17d3-4829-9729-d48c029211fe2",
    "ShopId": "636c08c4-600d-458a-98b7-8d312b8c18d2",

    "_rid": "2QZIAMVYbVQBAAAAAAAAAA==",
    "_self": "dbs/2QZIAA==/colls/2QZIAMVYbVQ=/docs/2QZIAMVYbVQBAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-4627-f721b0e701d6\"",
    "_attachments": "attachments/",
    "_ts": 1592564051
}

Any help or suggestions on how to resolve this would be much appreciated!任何有关如何解决此问题的帮助或建议将不胜感激!

Edit: The saving of the object to Cosmos is performed via:编辑:将 object 保存到 Cosmos 是通过以下方式执行的:

var response = _context.BikeRentals.Add(obj)
_context.SaveChanges();

Use Fluent API for model configuration in OnModelCreating method:OnModelCreating方法中使用 Fluent API 进行 model 配置:

modelBuilder.Entity<BikeRental>().Property(x => x.Id).ToJsonProperty("id");
modelBuilder.Entity<BikeRental>().Property(x => x.BikeId).ToJsonProperty("bikeId");
modelBuilder.Entity<BikeRental>().Property(x => x.ShopId).ToJsonProperty("shopId");

For EF mapping use ColumnAttribute or the fluent configuration in OnModelCreating is used.对于 EF 映射,使用 ColumnAttribute 或使用 OnModelCreating 中的流利配置。

In the case of Cosmos you may want the names in the database to be the same as the names when you serialize for sending to the client, but in the general case your entities may map to the database differently than for JSON serialization.对于 Cosmos,您可能希望数据库中的名称与序列化以发送到客户端时的名称相同,但在一般情况下,您的实体对数据库的 map 可能与 JSON 序列化不同。

You should use the Fluent API and iterate all the entity types, and properties an apply the Pascal-to-Camel-Case transformation.您应该使用Fluent API并迭代所有实体类型和属性,然后应用 Pascal-to-Camel-Case 转换。

The problem with the id is due to not adding id的问题是由于没有添加

  modelBuilder.Entity<BikeRental>().HasNoDiscriminator();

If you add this it should just create the id alone如果您添加它,它应该只创建 id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core Azure Cosmos DB Provider vs Cosmos LINQ 到 SQL 翻译 - EF Core Azure Cosmos DB Provider vs Cosmos LINQ to SQL translation 为什么 Azure 函数 JSON 序列化将 class 属性名称转换为驼峰和小写? - Why does Azure Functions JSON serialisation convert class property names to camel case and lower case? JSON 序列化中属性名称的默认驼峰式大小写 - Default camel case of property names in JSON serialization 使用Cosmos DB提供程序的EF Core中的自定义序列号生成器 - Custom sequence number generator in EF Core using Cosmos DB provider 为什么 SelectMany 不适用于 EF Core Cosmos DB? - Why SelectMany does not work for EF Core Cosmos DB? 使用 Cosmos DB 配置 EF Core 以存储字符串的 ICollection - Configure EF Core with Cosmos DB to store ICollection of string EF Core 与 Cosmos DB 提供程序,UserManager AddLoginAsync 提供 ConcurrencyFailure - EF Core with Cosmos DB provider, UserManager AddLoginAsync gives ConcurrencyFailure Entity Framework Core Cosmos Db - 如何比较字符串,不区分大小写? - Entity Framework Core Cosmos Db - How to compare strings, case insensitive? 如何使用Core(SQL)API从Azure Cosmos Db检索数据 - How to retrieve data from Azure Cosmos Db with Core(SQL) API 支持SignalR V 2.0中的驼峰案例属性名称 - Supporting camel case property names in SignalR V 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM