EF 6 IsRequired()允许空字符串 [英] EF 6 IsRequired() allowing empty strings
问题描述
在以前的版本为EF5和EF4的项目中,如果属性为null或空字符串,则IsRequired()流利的API方法将引发DbEntityValidationException.在我当前使用EF6的项目中,当string属性为空时,不会引发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屋!