Azure Cosmos DB(EF/Core)

编程入门 行业动态 更新时间:2024-10-20 16:38:16
本文介绍了Azure Cosmos DB(EF/Core)-骆驼案例属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个.NET Core 3.1 API项目,该项目具有通过实体框架(Microsoft.EntityFrameworkCore.Cosmos-v3.1.5)处理的Cosmos DB存储.

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).

我有一个数据库模型:

[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; } }

保存到CosmosDB数据库后,将使用类属性名称对列进行序列化,而忽略"PropertyName"属性.例如,如果将'bikeId'更改为'testBikeId',则其仍写为'BikeId'.

Upon saving to the CosmosDB database, the columns are being serialised using the class property names, ignoring the 'PropertyName' attribute. For example, if 'bikeId' is changed to 'testBikeId' it is written as 'BikeId' still.

{ "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!

通过以下方式将对象保存到Cosmos:

The saving of the object to Cosmos is performed via:

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

推荐答案

对于EF映射,请使用ColumnAttribute或OnModelCreating中的流畅配置.

For EF mapping use ColumnAttribute or the fluent configuration in OnModelCreating is used.

对于Cosmos,您可能希望数据库中的名称与序列化以发送给客户端时的名称相同,但是在一般情况下,您的实体映射到数据库的方式可能与JSON序列化的方式不同.

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.

您应该使用 Fluent API 并迭代所有实体类型,并应用Pascal到Camel-Case转换的属性.

You should use the Fluent API and iterate all the entity types, and properties an apply the Pascal-to-Camel-Case transformation.

更多推荐

Azure Cosmos DB(EF/Core)

本文发布于:2023-11-17 02:21:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608459.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Cosmos   Azure   DB   Core   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!