是否有“优雅”的方式给特定的属性一个默认值?
也许是DataAnnotations,比如:
[DefaultValue("true")]
public bool Active { get; set; }
谢谢你!
是否有“优雅”的方式给特定的属性一个默认值?
也许是DataAnnotations,比如:
[DefaultValue("true")]
public bool Active { get; set; }
谢谢你!
当前回答
假设您有一个名为Products的类名,并且有一个IsActive字段。你只需要一个create构造函数:
Public class Products
{
public Products()
{
IsActive = true;
}
public string Field1 { get; set; }
public string Field2 { get; set; }
public bool IsActive { get; set; }
}
那么您的IsActive默认值为True!
Edite:
如果你想用SQL来执行这个命令:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.IsActive)
.HasDefaultValueSql("true");
}
其他回答
在@SedatKapanoglu评论之后,我添加了我所有的方法,因为他是对的,只是使用流畅的API是行不通的。
1-创建自定义代码生成器和覆盖生成一个ColumnModel。
public class ExtendedMigrationCodeGenerator : CSharpMigrationCodeGenerator
{
protected override void Generate(ColumnModel column, IndentedTextWriter writer, bool emitName = false)
{
if (column.Annotations.Keys.Contains("Default"))
{
var value = Convert.ChangeType(column.Annotations["Default"].NewValue, column.ClrDefaultValue.GetType());
column.DefaultValue = value;
}
base.Generate(column, writer, emitName);
}
}
2-分配新的代码生成器:
public sealed class Configuration : DbMigrationsConfiguration<Data.Context.EfSqlDbContext>
{
public Configuration()
{
CodeGenerator = new ExtendedMigrationCodeGenerator();
AutomaticMigrationsEnabled = false;
}
}
3-使用fluent api创建注释:
public static void Configure(DbModelBuilder builder){
builder.Entity<Company>().Property(c => c.Status).HasColumnAnnotation("Default", 0);
}
在2016年6月27日发布的EF core中,您可以使用fluent API来设置默认值。转到ApplicationDbContext类,找到/创建方法名OnModelCreating并添加以下流畅的API。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<YourTableName>()
.Property(b => b.Active)
.HasDefaultValue(true);
}
using System.ComponentModel;
[DefaultValue(true)]
public bool Active { get; set; }
在。net Core 3.1中,你可以在模型类中做以下事情:
public bool? Active { get; set; }
在DbContext OnModelCreating中添加默认值。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Foundation>()
.Property(b => b.Active)
.HasDefaultValueSql("1");
base.OnModelCreating(modelBuilder);
}
在数据库中产生如下结果
注意: 如果你的属性没有nullable (bool?),你会得到以下警告
The 'bool' property 'Active' on entity type 'Foundation' is configured with a database-generated default. This default will always be used for inserts when the property has the value 'false', since this is the CLR default for the 'bool' type. Consider using the nullable 'bool?' type instead so that the default will only be used for inserts when the property value is 'null'.
Entity Framework Core Fluent API HasDefaultValue方法用于指定映射到属性的数据库列的默认值。该值必须为常数。
public class Contact
{
public int ContactId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public bool IsActive { get; set; }
public DateTime DateCreated { get; set; }
}
public clas SampleContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Context>()
.Propery(p => p.IsActive)
.HasDefaultValue(true);
}
}
Or
喜欢它!
你也可以指定一个SQL片段来计算默认值:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.Property(b => b.Created)
.HasDefaultValueSql("getdate()");
}