关于EF代码优先默认值的问题
/// <summary>
/// 是否是有效厂家(投标人)原因说明
/// </summary>
[MaxLength(300)]
[DefaultValue("")]
public string YNRemark { get; set; }
假如mode中我有以上字段,我想在数据库中同时也生默认值,类似如下
create table test(YNRemark varchar(300) not null default ''),请问我应该如何做?
我希望不要每个字段都去设置一变
最好如下面我指定的小数位例子
/// <summary>
/// 用于modelBuilder全局设置自定义精度属性
/// </summary>
public class DecimalPrecisionAttributeConvention
: PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>
{
public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)
{
if (attribute.Precision < 1 || attribute.Precision > 38)
{
throw new InvalidOperationException("Precision must be between 1 and 38.");
}
if (attribute.Scale > attribute.Precision)
{
throw new InvalidOperationException("Scale must be between 0 and the Precision value.");
}
configuration.HasPrecision(attribute.Precision, attribute.Scale);
}
}
在onmodelcreating加一句话就好了
modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//生成自己定义的表名
modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());
//设置字段映射
modelBuilder.Entity<Plan>()
.Property(u => u.StartDate)
.HasColumnName("StarDate");
//生成存储过程 var destinations = context.Database.SqlQuery<DestinationSummary>("dbo.GetDestinationSummary @p0, @p1", country, keyWords);
modelBuilder.Entity<SysLog>().MapToStoredProcedures();
}
...全文
相关推荐
DevelopmentWithADot.EntityFrameworkFilters:DevelopmentWithADot.EntityFrameworkFilters
DevelopmentWithADot.EntityFrameworkFilters 实体框架代码优先自动过滤器首先在讨论 版权所有里卡多佩雷斯 2013
2021-2022计算机二级等级考试试题及答案No.2801.docx
2021-2022计算机二级等级考试试题及答案No.2801.docx
EF代码优先还是模型优先还是数据库优先?
根据比较检查代码优先与模型优先/数据库优先[^],您可以更好地确定哪种情况适合您的情况。
EF代码优先实例(包含SQLServer与MySQL)
本实例包含EFCodeFirst分别SQLServer和MySQL数据的使用。Code First模式我们称之为“代码优先”模式,是从EF4.1开始新建加入的功能。使用Code First模式进行EF开发时开发人员只需要编写对应的数据类(其实就是领域模型的实现过程),然后自动生成数据库。
MVC+EF框架代码优先示例源码
ASP.NET MVC应用程序中使用实体框架代码优先学习源码


