php isoptional,entity-framework – 实体框架7 Fluent API无法识别IsOptional()

基于此

documentation page中的注释,似乎已经取消了以声明方式执行此操作的支持.以机智:

A property whose CLR type cannot contain null cannot be configured as optional. The property will always be considered required by Entity Framework.

这是故意的,可以在GitHub上的项目设计讨论中看到,specifically:

That is, a property marked as nullable supports null values, while a property marked as non-nullable must never contain null values. It follows from this that marking a property which is of a non-nullable CLR type as allowing nulls will not be allowed. This is different from the EF6 behavior where this is allowed. [emphasis added]

结果是,在EF7中,NULL列严格意味着可以为空的映射属性.如果您的属性可以为空,则关联列必须为NULL,除非您使用IsRequired标记或配置它.

对OP编辑的回应

这很有意思,我最初没有看到关于IsRequired(bool)API的文档.我在0700年6月的一些论文中找到了一个讨论点,它说明这相当于EF6的IsOptional():

.IsOptional() – We’ll provide this functionality via calling Required(false)

.IsRequired() – Provide Required() with the same functionality

即使这是最初的意图,取消支持的设计决定可以追溯到10月份. (每次更新)尝试在非可空属性上设置IsRequired(false)会导致运行时错误,而不是完全删除.

虽然现在是多余的,但是在不破坏有效代码的情况下无法删除API:它没有使用单独的IsRequired(bool)和IsRequired()定义实现,而是使用单个IsRequired(bool required = true).如果它被删除并替换为无参数版本,那将是一个重大变化.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值