EF CodeFirst 设置默认字符串长度并用 DataAnnotations 覆盖?
EF CodeFirst set default string length and override with DataAnnotations?
我的大多数字符串列都设置在 50 左右。
我可以将默认字符串生成设置为 50,而不是将 DataAnnotation [StringLength(50)]
添加到每个字符串 属性,并且只在需要不同于默认值时才指定 DataAnnotation 吗?
例如
[StringLength(200)]
public string Thing1 { get; set; }
public string Thing2 { get; set; }
public string Thing3 { get; set; }
[MaxLength]
public string Thing4 { get; set; }
在此示例中,默认情况下 Thing2 和 Thing3 可以是 varchar(50),而 Thing1 和 Thing2 会有所不同,因为我已经专门设置了其他方式
许多实体和列,因此这不仅可以节省我的时间,而且可以让我的实体 类 看起来更干净
澄清一下(为了可能重复的问题):
- 我不介意如何设置默认长度(FluentAPI 或其他)
- 我确实介意如何设置覆盖长度。我想使用 DataAnnotations
覆盖
您可以使用自定义代码优先约定。尝试将此添加到您的上下文 class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<string>()
.Configure(c => c.HasMaxLength(500));
}
查看此 link 了解有关 Custom Code First Conventions
的更多信息
是的,您可以使用自定义代码优先约定,但您还需要有一种方法将 nvarchar(max) 数据类型指定为字符串 属性。所以,我想出了以下解决方案。
/// <summary>
/// Set this attribute to string property to have nvarchar(max) type for db table column.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class TextAttribute : Attribute
{
}
/// <summary>
/// Changes all string properties without System.ComponentModel.DataAnnotations.StringLength or
/// Text attributes to use string length 16 (i.e nvarchar(16) instead of nvarchar(max) by default).
/// Use TextAttribute to a property to have nvarchar(max) data type.
/// </summary>
public class StringLength16Convention : Convention
{
public StringLength16Convention()
{
Properties<string>()
.Where(p => !p.GetCustomAttributes(false).OfType<DatabaseGeneratedAttribute>().Any())
.Configure(p => p.HasMaxLength(16));
Properties()
.Where(p => p.GetCustomAttributes(false).OfType<TextAttribute>().Any())
.Configure(p => p.IsMaxLength());
}
}
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Change string length default behavior.
modelBuilder.Conventions.Add(new StringLength16Convention());
}
}
public class LogMessage
{
[Key]
public Guid Id { get; set; }
[StringLength(25)] // Explicit data length. Result data type is nvarchar(25)
public string Computer { get; set; }
//[StringLength(25)] // Implicit data length. Result data type is nvarchar(16)
public string AgencyName { get; set; }
[Text] // Explicit max data length. Result data type is nvarchar(max)
public string Message { get; set; }
}
从 Entity Frameworks Core 6 开始,现在您可以覆盖 ConfigureConventions DbContext,根据 this site
为给定的 属性 类型设置默认配置
示例:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// Pre-convention model configuration goes here
configurationBuilder.Properties<string>().HaveMaxLength(300);
}
我的大多数字符串列都设置在 50 左右。
我可以将默认字符串生成设置为 50,而不是将 DataAnnotation [StringLength(50)]
添加到每个字符串 属性,并且只在需要不同于默认值时才指定 DataAnnotation 吗?
例如
[StringLength(200)]
public string Thing1 { get; set; }
public string Thing2 { get; set; }
public string Thing3 { get; set; }
[MaxLength]
public string Thing4 { get; set; }
在此示例中,默认情况下 Thing2 和 Thing3 可以是 varchar(50),而 Thing1 和 Thing2 会有所不同,因为我已经专门设置了其他方式
许多实体和列,因此这不仅可以节省我的时间,而且可以让我的实体 类 看起来更干净
澄清一下(为了可能重复的问题): - 我不介意如何设置默认长度(FluentAPI 或其他) - 我确实介意如何设置覆盖长度。我想使用 DataAnnotations
覆盖您可以使用自定义代码优先约定。尝试将此添加到您的上下文 class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<string>()
.Configure(c => c.HasMaxLength(500));
}
查看此 link 了解有关 Custom Code First Conventions
的更多信息是的,您可以使用自定义代码优先约定,但您还需要有一种方法将 nvarchar(max) 数据类型指定为字符串 属性。所以,我想出了以下解决方案。
/// <summary>
/// Set this attribute to string property to have nvarchar(max) type for db table column.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class TextAttribute : Attribute
{
}
/// <summary>
/// Changes all string properties without System.ComponentModel.DataAnnotations.StringLength or
/// Text attributes to use string length 16 (i.e nvarchar(16) instead of nvarchar(max) by default).
/// Use TextAttribute to a property to have nvarchar(max) data type.
/// </summary>
public class StringLength16Convention : Convention
{
public StringLength16Convention()
{
Properties<string>()
.Where(p => !p.GetCustomAttributes(false).OfType<DatabaseGeneratedAttribute>().Any())
.Configure(p => p.HasMaxLength(16));
Properties()
.Where(p => p.GetCustomAttributes(false).OfType<TextAttribute>().Any())
.Configure(p => p.IsMaxLength());
}
}
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Change string length default behavior.
modelBuilder.Conventions.Add(new StringLength16Convention());
}
}
public class LogMessage
{
[Key]
public Guid Id { get; set; }
[StringLength(25)] // Explicit data length. Result data type is nvarchar(25)
public string Computer { get; set; }
//[StringLength(25)] // Implicit data length. Result data type is nvarchar(16)
public string AgencyName { get; set; }
[Text] // Explicit max data length. Result data type is nvarchar(max)
public string Message { get; set; }
}
从 Entity Frameworks Core 6 开始,现在您可以覆盖 ConfigureConventions DbContext,根据 this site
为给定的 属性 类型设置默认配置示例:
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// Pre-convention model configuration goes here
configurationBuilder.Properties<string>().HaveMaxLength(300);
}