映射到 CosmosDB 集合时,有什么方法可以为 Entity Framework Core 实体的属性设置默认值
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是文档中提到的。您可以在此处进一步检查 https: //learn.microsoft.com/en-us/ef/core/modeling/ generated-properties?tabs=data-annotations
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
您要替换,而不是默认值。在数据库中,默认值是当未提供其他值时存储的值。如果没有可用的值,则使用
null
。 null不是一个值,这意味着根本没有 no 值。如果要返回false
当没有可用值时,实际上您要求用非常特定的值替换缺失值。这样做可以轻松引入错误。它也会干扰查询。搜索
iSACTIVE = false
在说:它不会返回没有
ISACTIVE
值的对象。即使您将null
用false
替换为,您仍然必须查询null
,如果应用程序的业务逻辑要将null等于false等于false,它应该显式。一种方法是将自动属性替换为一个由可确定字段备份的自动属性,其getter替换为
null
`false:false:false: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 returnfalse
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:That won't return objects that just don't have an
IsActive
value. Even if you replaceNULL
withfalse
you'll still have to query forNULL
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: