如何禁用可选的依赖实体
How to disable optional dependent entities
我有以下两个实体引用一个 table 使用来自 EF Core 的拆分 table 选项 - 这个 非常 简化版本:
class Account
int Id
Settings Settings
class Settings
int AccountId (maps to Id)
string PropertyX
If all of the columns used by a dependent entity are NULL in the
database, then no instance for it will be created when queried. This
allows modeling an optional dependent entity, where the relationship
property on the principal would be null. Note that this would also
happen if all of the dependent's properties are optional and set to
null, which might not be expected.
是否可以禁用此行为?我有多个列,其中包含许多默认为 null 的分组行为。
现在默认不会创建实体(设置)。这意味着我必须在所有地方进行 nullcheck。我宁愿使用所有属性的空值创建设置。
如果我自己在 父实体(帐户)的构造函数中创建实例,则更改似乎不会被跟踪,因为我猜 EF Core 不知道class.
有什么解决办法吗?
遗憾的是,此功能在 EF Core 3 中不可用。
EF Core 5.0 中添加了所谓的必需依赖项 - Required 1:1 dependents:
In EF Core 3.1, the dependent end of a one-to-one relationship was always considered optional. This was most apparent when using owned entities, as all the owned entity's column were created as nullable in the database, even if they were configured as required in the model.
In EF Core 5.0, a navigation to an owned entity can be configured as a required dependent
以上只是新功能部分的功能公告。事实上,它可以用于任何一对一的关系,正如官方 One-to-one 文档部分所述:
The dependent side is considered optional by default, but can be configured as required. However EF will not validate whether a dependent entity was provided, so this configuration will only make a difference when the database mapping allows it to be enforced. A common scenario for this are reference owned types that use table splitting by default
要根据需要配置依赖项,您必须使用 Navigation
fluent API(也在 5.0 中引入)结合 IsRequired
:
modelBuilder.Entity<Account>()
.Navigation(e => e.Settings)
.IsRequired();
我有以下两个实体引用一个 table 使用来自 EF Core 的拆分 table 选项 - 这个 非常 简化版本:
class Account
int Id
Settings Settings
class Settings
int AccountId (maps to Id)
string PropertyX
If all of the columns used by a dependent entity are NULL in the database, then no instance for it will be created when queried. This allows modeling an optional dependent entity, where the relationship property on the principal would be null. Note that this would also happen if all of the dependent's properties are optional and set to null, which might not be expected.
是否可以禁用此行为?我有多个列,其中包含许多默认为 null 的分组行为。
现在默认不会创建实体(设置)。这意味着我必须在所有地方进行 nullcheck。我宁愿使用所有属性的空值创建设置。
如果我自己在 父实体(帐户)的构造函数中创建实例,则更改似乎不会被跟踪,因为我猜 EF Core 不知道class.
有什么解决办法吗?
遗憾的是,此功能在 EF Core 3 中不可用。
EF Core 5.0 中添加了所谓的必需依赖项 - Required 1:1 dependents:
In EF Core 3.1, the dependent end of a one-to-one relationship was always considered optional. This was most apparent when using owned entities, as all the owned entity's column were created as nullable in the database, even if they were configured as required in the model.
In EF Core 5.0, a navigation to an owned entity can be configured as a required dependent
以上只是新功能部分的功能公告。事实上,它可以用于任何一对一的关系,正如官方 One-to-one 文档部分所述:
The dependent side is considered optional by default, but can be configured as required. However EF will not validate whether a dependent entity was provided, so this configuration will only make a difference when the database mapping allows it to be enforced. A common scenario for this are reference owned types that use table splitting by default
要根据需要配置依赖项,您必须使用 Navigation
fluent API(也在 5.0 中引入)结合 IsRequired
:
modelBuilder.Entity<Account>()
.Navigation(e => e.Settings)
.IsRequired();