EF 6 IsRequired() 允许空字符串 [英] EF 6 IsRequired() allowing empty strings
问题描述
在过去使用 EF5 和 EF4 版本的项目中,如果属性为 null 或空字符串,IsRequired() fluent API 方法将引发 DbEntityValidationException.在我当前使用 EF6 的项目中,当字符串属性为空时,不会引发 DBEntityValidationException.
In past projects with versions of EF5 and EF4, the IsRequired() fluent API method would thrown a DbEntityValidationException if the property was null or an empty string. In my current project utilizng EF6, The DBEntityValidationException is not thrown when the string property is empty.
实体:
public class Application : BaseEntity
{
public string Name { get; set; }
// navigation properties
public IList<Role> Roles { get; set; }
}
配置:
internal class ApplicationMapping : EntityTypeConfiguration<Application>
{
public ApplicationMapping()
{
// table name
this.ToTable("Applications");
// properties
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(100);
}
}
在翻阅 MSDN EF 文档和堆栈溢出之后,我不知道为什么会发生这种情况.是否向 EF6 添加/修改了约定?
After pouring over the MSDN EF documentation and stack overflow, I am at a loss for why this is happening. Did a convention get added/modified to EF6?
推荐答案
这些天你仍然可以使用 [Required]
属性并具有可配置的 AllowEmptyStrings
These days you can still use [Required]
attribute and have configurable AllowEmptyStrings
[Required(AllowEmptyStrings = false)]
默认为假
这篇关于EF 6 IsRequired() 允许空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!