پشتیبانی از نوع داده JSON در EF Core 10: تحولی در مدیریت دادههای ساختیافته
نویسنده: وحید نصیری
تاریخ: ۱۴۰۴/۰۸/۱۰ ۱۱:۰۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
JSON_VALUE([Column], '$.Path').ToJson() برای Complex Properties (ویژگیهای پیچیده)، همه چیز سادهتر شده. این روش اجازه میدهد آبجکتها و آرایهها را مستقیماً به ستون JSON مپ کنید. این ویژگی روی SQL Server ۲۰۲۵ (با سطح سازگاری ۱۷۰) و Azure SQL Database کار میکند، اما حتی روی نسخههای قدیمیتر SQL Server (مانند ۲۰۲۲ با سطح سازگاری ۱۶۰) هم با استفاده از nvarchar(max) و توابع JSON_MODIFY قابل استفاده است. همچنین، پشتیبانی مشابهی برای PostgreSQL و SQLite وجود دارد، که کوئری روی فیلدهای JSON را بدون deserialize امکانپذیر میکند.public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public required Contact Contact { get; set; }
}
public class Contact
{
public required Address Address { get; set; }
public List<PhoneNumber> PhoneNumbers { get; set; } = new List<PhoneNumber>();
}
public class Address
{
public required string Street { get; set; }
public required string City { get; set; }
public required string State { get; set; }
public required string PostalCode { get; set; }
}
public class PhoneNumber
{
public PhoneNumberType Type { get; set; }
public string Number { get; set; }
public PhoneNumber(PhoneNumberType type, string number)
{
Type = type;
Number = number;
}
}
public enum PhoneNumberType
{
Mobile,
Home
}public class MyDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("<ConnectionString>", x => x.UseCompatibilityLevel(170)); // برای SQL Server 2025
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(x =>
{
x.ComplexProperty(c => c.Contact, b => b.ToJson());
});
}
}CREATE TABLE [Customers] (
[Id] int NOT NULL IDENTITY,
[Name] nvarchar(max) NOT NULL,
[Contact] json NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY ([Id])
);var customersInWA = await context.Customers
.Where(c => c.Contact.Address.State == "WA")
.ToListAsync();SELECT [c].[Id], [c].[Name], [c].[Contact] FROM [Customers] AS [c] WHERE JSON_VALUE([c].[Contact], '$.Address.State') = N'WA'
var distinctStates = await context.Customers
.Select(c => c.Contact.Address.State)
.Distinct()
.ToListAsync();SELECT DISTINCT JSON_VALUE([c].[Contact], '$.Address.State') FROM [Customers] AS [c]
EF.Functions.JsonContains استفاده کنید:var premiumUsers = db.Users
.Where(u => EF.Functions.JsonContains(u.Metadata, "{ \"Premium\": true }"))
.ToList();ExecuteUpdateAsync است. این روش فقط بخش مورد نظر JSON را تغییر میدهد:await context.Customers
.Where(c => c.Name == "John Doe")
.ExecuteUpdateAsync(setters => setters.SetProperty(c => c.Contact.Address.PostalCode, "98102"));UPDATE [c] SET [Contact] = JSON_MODIFY([Contact], '$.Address.PostalCode', @p0) FROM [Customers] AS [c] WHERE [c].[Name] = N'John Doe'
CREATE INDEX idx_State ON Customers ((JSON_VALUE(Contact, '$.Address.State')))؛ این کار جستجوها را سریعتر میکند، چیزی که در روشهای قدیمی سخت بود.