JsonProperty.EFCore
2.7.0
dotnet add package JsonProperty.EFCore --version 2.7.0
NuGet\Install-Package JsonProperty.EFCore -Version 2.7.0
<PackageReference Include="JsonProperty.EFCore" Version="2.7.0" />
paket add JsonProperty.EFCore --version 2.7.0
#r "nuget: JsonProperty.EFCore, 2.7.0"
// Install JsonProperty.EFCore as a Cake Addin #addin nuget:?package=JsonProperty.EFCore&version=2.7.0 // Install JsonProperty.EFCore as a Cake Tool #tool nuget:?package=JsonProperty.EFCore&version=2.7.0
JsonProperty.EFCore
About
This project allows you to use JSON fields in EF Core without setting up Fluent API
Id | Name | Price | Params |
---|---|---|---|
1 | Phone | 500 | {"Camera":13.5,"OS":"Android 11","Screen":"1080x900","Storage":32} |
2 | Car | 100000 | {"MaxSpeed":300,"Engine capacity":6,"ElectroCar":false} |
3 | Bag | 400 | {"Voliume":5,"Color":"Red"} |
Instruction
Here are few steps how to use JsonProperty.EFCore project:
Connect the project and specify 'using'
using JsonProperty.EFCore;
Create entity model
public class Product { [Key, Required] public int Id { get; set; } public string? Name { get; set; } public decimal? Price { get; set; } }
Or
public class Note { [Key, Required] public int Id { get; set; } public string? Header { get; set; } }
Create a JSON collection item class if necessary
public class TodoItem { public string? Text { get; set; } public bool? CompleteStatus { get; set; } }
Add JSON field property of type
JsonEnumerable
,JsonList
,JsonDictionary
orJsonItem
to your entity modelYou can use the basic type like
JsonEnumerable
orJsonDictionary
which uses elements of type objectpublic class Product { [Key, Required] public int Id { get; set; } public string? Name { get; set; } public decimal? Price { get; set; } public JsonDictionary Parameters { get; set; } = new(); }
Or add property of generic type like
JsonEnumerable<T>
orJsonDictionary<TKey, TValue>
with custom element typepublic class Note { [Key, Required] public int Id { get; set; } public string? Header { get; set; } public JsonEnumerable<TodoItem> Todos { get; set; } = new(); }
Usage example:
Here is example for
JsonDictionary
Product product = new() {Name="Phone",Price=500.95m,Amount=21,Parameters={ VirtualDictionary = new Dictionary<string,object>() { {"Camera",13.5 },{"OS","Android" },{"Screen","1080x900"},{"Storage",32} } }}; db.Goods.Add(product); db.SaveChanges();
This will generate the following JSON data into the corresponding table string field:
{ "Camera": 13.5, "OS": "Android", "Screen": "1080x900", "Storage": 32 }
Or next if
JsonSettings.StrictTypeSerialization
is 'true' (default){ "Camera": [13.5, "System.Double"], "OS": ["Android", "System.String"], "Screen": ["1080x900", "System.String"], "Storage": [32, "System.Int32"] }
And here is example for
JsonEnumerable<T>
Note note = db.Notes.FirstOrDefault(); note.Todos.Edit(en => en.Append(new("MyTodoItemTitle")));
And here is also the result in JSON:
[ ... , { "Text": "MyTodoItemTitle", "CompleteStatus": false }]
Or
[ { "Text": ["MyTodoItemTitle", "System.String"], "CompleteStatus": [false, "System.Boolean"] } ]
You can see more at wiki
Show your support
⭐️ this repository if this package helped you!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.9)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.