gpt4 book ai didi

entity-framework - EF 6 IsRequired()允许空字符串

转载 作者:行者123 更新时间:2023-12-03 21:04:15 25 4
gpt4 key购买 nike

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

实体:

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添加或修改了约定?

最佳答案

这些天,您仍然可以使用[Required]属性并具有可配置的AllowEmptyStrings

[Required(AllowEmptyStrings = false)]


默认为False

关于entity-framework - EF 6 IsRequired()允许空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20619632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com