Язык программирования C#9 и платформа .NET5 - Троелсен Эндрю
На нашем сайте KnigaRead.com Вы можете абсолютно бесплатно читать книгу онлайн Троелсен Эндрю, "Язык программирования C#9 и платформа .NET5" бесплатно, без регистрации.
using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;using System.Xml.Linq;namespace AutoLot.Models.Entities{ [Table("SeriLogs", Schema = "Logging")] public class SeriLogEntry { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string? Message { get; set; } public string? MessageTemplate { get; set; } [MaxLength(128)] public string? Level { get; set; } [DataType(DataType.DateTime)] public DateTime? TimeStamp { get; set; } public string? Exception { get; set; } public string? Properties { get; set; } public string? LogEvent { get; set; } public string? SourceContext { get; set; } public string? RequestPath { get; set; } public string? ActionName { get; set; } public string? ApplicationName { get; set; } public string? MachineName { get; set; } public string? FilePath { get; set; } public string? MemberName { get; set; } public int? LineNumber { get; set; } [NotMapped] public XElement? PropertiesXml => (Properties != null)? XElement.Parse(Properties):null; }}Итак, сущностный класс
SeriLogEntryНа заметку! Свойство
TimeStampSeriLogEntryTimeStampBaseEntityrowversionКласс ApplicationDbContext
Пришло время обновить файл
ApplicationDbContext.csusingusing System;using System.Collections;using System.Collections.Generic;using AutoLot.Models.Entities;using AutoLot.Models.Entities.Owned;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Storage;using Microsoft.EntityFrameworkCore.ChangeTracking;using AutoLot.Dal.Exceptions;Файл начинается с конструктора без параметров. Удалите его, т.к. он не нужен. Следующий конструктор принимает экземпляр
DbContextOptionsDbContextChangeTrackerСвойства
DbSet<T>nullvirtualDbSet<T><b>public DbSet<SeriLogEntry>? LogEntries { get; set; }</b>public DbSet<CreditRisk>? CreditRisks { get; set; }public DbSet<Customer>? Customers { get; set; }public DbSet<Make>? Makes { get; set; }public DbSet<Car>? Cars { get; set; }public DbSet<Order>? Orders { get; set; }Обновление кода Fluent API
Код Fluent API находится в переопределенной версии метода
OnModelCreating()ModelBuilderСущность SeriLogEntry
Первое изменение, вносимое в метод
OnModelCreating()SeriLogEntryPropertiesTimeStampdatetime2getdate()OnModelCreating()modelBuilder.Entity<SeriLogEntry>(entity =>{ entity.Property(e => e.Properties).HasColumnType("Xml"); entity.Property(e => e.TimeStamp).HasDefaultValueSql("GetDate()");});Сущность CreditRisk
Далее понадобится модифицировать код сущности
CreditRiskTimeStampBaseEntitynullFirstNameLastNameFullNameCreditRisk