映射到 CosmosDB 集合时,有什么方法可以为 Entity Framework Core 实体的属性设置默认值

发布于 2025-01-20 20:45:29 字数 829 浏览 0 评论 0原文

我在 Cosmos DB 中有一个集合Collection-test。我的集合实体是:

public class Collection-test
{
    public string id { get; set; } = null!;
    public bool isActive { get; set; }
}

我的 EF 映射是:

modelBuilder.Entity<Collection-test>(entity =>
    {
        entity.ToContainer("Collection-test");
        entity.HasNoDiscriminator();
        entity.Property(e => e.isActive).HasConversion<bool>();
        entity.HasPartitionKey(x => x.id);
    });

我的问题是:集合中的某些文档没有 isActive 键。因此,如果实体类中的 isActive 字段不可为 null,则 null 赋值会导致异常。

但如果我使用

public bool? isActive { get; set; } 

而不是

public bool isActive { get; set; } 

映射工作正常。

如果实体类中的字段可为空,有人能找到一种分配默认值的方法吗?

I have a collection Collection-test in Cosmos DB. My entity for the collection is:

public class Collection-test
{
    public string id { get; set; } = null!;
    public bool isActive { get; set; }
}

My EF mapping is:

modelBuilder.Entity<Collection-test>(entity =>
    {
        entity.ToContainer("Collection-test");
        entity.HasNoDiscriminator();
        entity.Property(e => e.isActive).HasConversion<bool>();
        entity.HasPartitionKey(x => x.id);
    });

My issue is: some of documents in the collection have no isActive key. So if isActive field is not nullable in the entity class, then null assignment causes an exception.

But if I use

public bool? isActive { get; set; } 

instead of

public bool isActive { get; set; } 

the mapping works fine.

Could anybody find a way to assign default value if the field it is nullable in the entity class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2025-01-27 20:45:29
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Collection-test>()
        .Property(b => b.IsActive)
        .HasDefaultValue(false);
}

这是文档中提到的。您可以在此处进一步检查 https: //learn.microsoft.com/en-us/ef/core/modeling/ generated-properties?tabs=data-annotations

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Collection-test>()
        .Property(b => b.IsActive)
        .HasDefaultValue(false);
}

This is what mentioned in docs. You can check it further here https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations

流年里的时光 2025-01-27 20:45:29

您要替换,而不是默认值。在数据库中,默认值是当未提供其他值时存储的值。如果没有可用的值,则使用null。 null不是一个值,这意味着根本没有 no 值。如果要返回false当没有可用值时,实际上您要求用非常特定的值替换缺失值。

这样做可以轻松引入错误。它也会干扰查询。搜索iSACTIVE = false在说:

获取具有iSACTIVE的字段,其值为false

它不会返回没有ISACTIVE值的对象。即使您将nullfalse替换为,您仍然必须查询null

如果应用程序的业务逻辑要将null等于false等于false,它应该显式。一种方法是将自动属性替换为一个由可确定字段备份的自动属性,其getter替换为null`false:false:false:

bool? _isActive;
public bool IsActive 
{ 
    get => _isActive??false; 
    set => _isActive=value;
}

You're asking for a replacement, not a default value. In databases, the default is the value that's stored when no other value is provided. When no value is available, NULL is used. NULL isn't a value, it means there's no value at all. If you want to return false when no value is available you're actually asking to replace the missing value with a very specific one.

Doing this can easily introduce errors. It will interfere with querying as well. Searching for IsActive = False is saying:

fetch objects that have an IsActive field whose value is False

That won't return objects that just don't have an IsActive value. Even if you replace NULL with false you'll still have to query for NULL

If the application's business logic wants to treat null equal to false, it should make this explicit. One way to do this is to replace the automatic property with one backed by a nullable field whose getter replaces null with `false:

bool? _isActive;
public bool IsActive 
{ 
    get => _isActive??false; 
    set => _isActive=value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文