EF8 处理字符串可为空性:必需、设置与 init

问题描述 投票:0回答:1

EF8 中应如何处理

string
可空性?我应该使用
required
关键字吗?我还应该选择
set;
而不是
init;
吗?我记得 .NET 6 中的
init;
有一些问题。

public class LegalEntity : BaseAuditableEntity<Guid>
{
    // This is required by EF Core when there is another ctor with parameters that we need to pass
    [UsedImplicitly]
    private LegalEntity()
    {
    }

    // TODO: Add ctor with couple of parameters

    // TODO: How are we supposed to handle string nullability in .NET 8 and EF Core?
    // TODO: Should we use init instead?
    // https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types
    public required string EntityType { get; set; }
    public required string Name { get; set; }
    public required string ShortName { get; set; }
    public required string RegistrationNumber { get; set; }
    public DateTime? DateOfIncorporation { get; set; }

    public required string StreetAddress { get; set; }
    public required string City { get; set; }
    public required string State { get; set; }
    public required string PostalCode { get; set; }
    public required string Country { get; set; }
}

public class LegalEntityEfConfiguration : IEntityTypeConfiguration<LegalEntity>
{
    public void Configure(EntityTypeBuilder<LegalEntity> builder)
    {
        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id).ValueGeneratedOnAdd();

        builder.Property(x => x.EntityType).IsRequired().HasMaxLength(50);
        builder.Property(x => x.Name).IsRequired().HasMaxLength(255);
        builder.Property(x => x.ShortName).HasMaxLength(100);
        builder.Property(x => x.RegistrationNumber).HasMaxLength(100);

        builder.Property(x => x.StreetAddress).HasMaxLength(255);
        builder.Property(x => x.City).HasMaxLength(100);
        builder.Property(x => x.State).HasMaxLength(100);
        builder.Property(x => x.PostalCode).HasMaxLength(20);
        builder.Property(x => x.Country).HasMaxLength(100);
    }
}

public abstract class BaseAuditableEntity<TId> : BaseEntity<TId>
    where TId : struct
{
    public DateTimeOffset CreatedAt { get; set; }

    public string? CreatedBy { get; set; }

    public DateTimeOffset UpdatedAt { get; set; }

    public string? UpdatedBy { get; set; }
}

public abstract class BaseEntity<TId>
    where TId : struct
{
    public TId Id { get; set; }

    private readonly List<BaseEvent> _domainEvents = [];

    [NotMapped]
    public IReadOnlyCollection<BaseEvent> DomainEvents => _domainEvents.AsReadOnly();

    public void AddDomainEvent(BaseEvent domainEvent) => _domainEvents.Add(domainEvent);

    public void RemoveDomainEvent(BaseEvent domainEvent) => _domainEvents.Remove(domainEvent);

    public void ClearDomainEvents() => _domainEvents.Clear();
}
.net asp.net-core entity-framework-core .net-8.0 ef-core-8.0
1个回答
0
投票

您可以使用

required
在初始化期间设置不可为 null 的属性。

这是示例:

public required string EntityType { get; set; }
public required string Name { get; set; }
public string? RegistrationNumber { get; set; }  // Optional property
  • 使用

    init;
    允许您仅在对象初始化期间为只读属性赋值。我们可以使用此功能来创建不可变对象,这些对象在创建过程中需要一组特定的值,并且这些值在对象的生命周期内应保持不变。

    您只能在对象创建期间调用

    init
    访问器。

  • 如果在构造对象后可能需要修改属性,请使用

    set;

使用 EF Core 时,数据加载和更新期间依赖于属性设置器,最好对将由框架更新的属性使用

set;

例如:

public required string EntityType { get; set; }  // Allows updates

以下是您可以参考的文档:

EF Core 中的可空引用类型

© www.soinside.com 2019 - 2024. All rights reserved.