小编awo*_*ske的帖子

EF 6 IsRequired()允许空字符串

在以前使用EF5和EF4版本的项目中,如果属性为null或为空字符串,则IsRequired()流畅的API方法将抛出DbEntityValidationException.在我当前的项目utilizng EF6中,当string属性为空时,不会抛出DBEntityValidationException.

实体:

public class Application : BaseEntity
{
    public string Name { get; set; }

    // navigation properties
    public IList<Role> Roles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

组态:

internal class ApplicationMapping : EntityTypeConfiguration<Application>
{
    public ApplicationMapping()
    {
        // table name
        this.ToTable("Applications");

        // properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(100);
    }
}
Run Code Online (Sandbox Code Playgroud)

在倾注了MSDN EF文档和堆栈溢出后,我不知道为什么会发生这种情况.是否已将约定添加/修改为EF6?

entity-framework entity-framework-6

14
推荐指数
2
解决办法
7574
查看次数