How can I set the default value using EntityFramework Code First FluentAPI for bool property?
我如何使用EntityFramework代码首先为bool属性设置默认值?
Something like:
喜欢的东西:
Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);
4 个解决方案
#1
24
Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax:
好消息,代码首先支持这个。在生成的迁移的“Up()”方法中,使用以下语法指定默认值:
AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false));
MSDN“AddColumn”方法
#2
8
I'm not sure about a fluent way, but you can simply set the property in a parameterless constructor...
我不确定是否可以使用流畅的方式,但是您可以简单地将属性设置为无参数的构造函数……
public class MyTable
{
public MyTable()
{
CanSetDefault = true;
}
public bool CanSetDefault {get; set; }
}
Update
更新
A quick google suggests it is not possible using the fluent api...
http://social.msdn.microsoft.com/Forums/en-US/ad854e28-02f5-451b-9000-c8bcb1355d0b/codefirst-ctp5-and-default-values?forum=adonetefx
一个快速的谷歌表明,使用fluent api是不可能的……http://social.msdn.microsoft.com/forums/en - us/ad854e28 - 02 - f5 - 451 - b - 9000 - c8bcb1355d0b/codefirst ctp5 values?forum=adonetefx——和——违约
#3
1
Since EF doesn't have the functions I need, such as default values and unique key as foreign keys, we have to change the ORM from EF to NHibernate. It seems to me that NHibernate has more functions than EF 6.X.
由于EF没有我所需要的函数,比如默认值和惟一键作为外键,所以我们必须将ORM从EF更改为NHibernate。在我看来,NHibernate的功能比EF 6.X更多。
#4
1
Another option here is to override the default SqlServerMigrationSqlGenerator class with your own. You can then inject certain things you want to happen in the Generate method (for example, default values). The nice things with this is that you can also use this in other applications as it is pretty generic. Here is a good explanation on it.
这里的另一个选项是用您自己的方法覆盖默认的SqlServerMigrationSqlGenerator类。然后,您可以在Generate方法(例如,默认值)中注入一些想要发生的事情。这样做的好处是可以在其他应用程序中使用它,因为它非常通用。这里有一个很好的解释。
internal class CustomSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
SetCreatedUtcColumn(addColumnOperation.Column);
base.Generate(addColumnOperation);
}
protected override void Generate(CreateTableOperation createTableOperation)
{
SetCreatedUtcColumn(createTableOperation.Columns);
base.Generate(createTableOperation);
}
private static void SetCreatedUtcColumn(IEnumerable<ColumnModel> columns)
{
foreach (var columnModel in columns)
{
SetCreatedUtcColumn(columnModel);
}
}
private static void SetCreatedUtcColumn(PropertyModel column)
{
if (column.Name == "CreatedUtc")
{
column.DefaultValueSql = "GETUTCDATE()";
}
}
}
#1
24
Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax:
好消息,代码首先支持这个。在生成的迁移的“Up()”方法中,使用以下语法指定默认值:
AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false));
MSDN“AddColumn”方法
#2
8
I'm not sure about a fluent way, but you can simply set the property in a parameterless constructor...
我不确定是否可以使用流畅的方式,但是您可以简单地将属性设置为无参数的构造函数……
public class MyTable
{
public MyTable()
{
CanSetDefault = true;
}
public bool CanSetDefault {get; set; }
}
Update
更新
A quick google suggests it is not possible using the fluent api...
http://social.msdn.microsoft.com/Forums/en-US/ad854e28-02f5-451b-9000-c8bcb1355d0b/codefirst-ctp5-and-default-values?forum=adonetefx
一个快速的谷歌表明,使用fluent api是不可能的……http://social.msdn.microsoft.com/forums/en - us/ad854e28 - 02 - f5 - 451 - b - 9000 - c8bcb1355d0b/codefirst ctp5 values?forum=adonetefx——和——违约
#3
1
Since EF doesn't have the functions I need, such as default values and unique key as foreign keys, we have to change the ORM from EF to NHibernate. It seems to me that NHibernate has more functions than EF 6.X.
由于EF没有我所需要的函数,比如默认值和惟一键作为外键,所以我们必须将ORM从EF更改为NHibernate。在我看来,NHibernate的功能比EF 6.X更多。
#4
1
Another option here is to override the default SqlServerMigrationSqlGenerator class with your own. You can then inject certain things you want to happen in the Generate method (for example, default values). The nice things with this is that you can also use this in other applications as it is pretty generic. Here is a good explanation on it.
这里的另一个选项是用您自己的方法覆盖默认的SqlServerMigrationSqlGenerator类。然后,您可以在Generate方法(例如,默认值)中注入一些想要发生的事情。这样做的好处是可以在其他应用程序中使用它,因为它非常通用。这里有一个很好的解释。
internal class CustomSqlServerMigrationSqlGenerator : SqlServerMigrationSqlGenerator
{
protected override void Generate(AddColumnOperation addColumnOperation)
{
SetCreatedUtcColumn(addColumnOperation.Column);
base.Generate(addColumnOperation);
}
protected override void Generate(CreateTableOperation createTableOperation)
{
SetCreatedUtcColumn(createTableOperation.Columns);
base.Generate(createTableOperation);
}
private static void SetCreatedUtcColumn(IEnumerable<ColumnModel> columns)
{
foreach (var columnModel in columns)
{
SetCreatedUtcColumn(columnModel);
}
}
private static void SetCreatedUtcColumn(PropertyModel column)
{
if (column.Name == "CreatedUtc")
{
column.DefaultValueSql = "GETUTCDATE()";
}
}
}