我已经将[Required]数据注释添加到ASP.NET MVC应用程序中的一个模型中.创建迁移后,运行该Update-Database命令会导致以下错误:
无法将值NULL插入列'Director',表'MOVIES_cf7bad808fa94f89afa2e5dae1161e78.dbo.Movies'; 列不允许空值.更新失败.该语句已终止.
这是因为某些记录的Director列中包含NULL .如何自动将这些值更改为默认值(例如"John Doe")导演?
这是我的模型:
public class Movie
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required]
public string Genre { get; set; }
[Range(1,100)]
[DataType(DataType.Currency)]
public decimal Price { get; set; }
[StringLength(5)]
public string Rating { get; set; }
[Required] /// <--- NEW
public string Director { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 我有一个使用OOB数据库初始化程序创建的数据库,我使用的是Code First和EF 4.3.1.
我想利用Add-Migration cmdlet上的新"IgnoreChanges"标志,以便我可以更改一些列并添加默认SQL值.本质上,我的一些实体有一个名为DateLastUpdated的列,我想将DEFAULT设置为sql表达式GETDATE().
我使用"add-migration InitialMigration -ignorechanges"创建了InitialMigration,然后我将以下内容添加到Up()和Down():
public override void Up()
{
AlterColumn("CustomerLocations", "DateLastUpdated", c => c.DateTime(defaultValueSql: "GETDATE()"));
AlterColumn("UserReportTemplates", "DateLastUpdated", c => c.DateTime(defaultValueSql: "GETDATE()"));
AlterColumn("Chains", "DateLastUpdated", c => c.DateTime(defaultValueSql: "GETDATE()"));
}
public override void Down()
{
AlterColumn("CustomerLocations", "DateLastUpdated", c => c.DateTime());
AlterColumn("UserReportTemplates", "DateLastUpdated", c => c.DateTime());
AlterColumn("Chains", "DateLastUpdated", c => c.DateTime());
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试运行"Update-Database -verbose",但我发现它正在尝试在数据库上创建相同的命名默认约束,并且SQL抛出异常:
Applying explicit migrations: [201203221856095_InitialMigration].
Applying explicit migration: 201203221856095_InitialMigration.
ALTER TABLE [CustomerLocations] ADD CONSTRAINT DF_DateLastUpdated DEFAULT GETDATE() FOR [DateLastUpdated]
ALTER TABLE [CustomerLocations] ALTER COLUMN [DateLastUpdated] [datetime] …Run Code Online (Sandbox Code Playgroud) 我发现的所有关于声明默认值的方法都是在 Sql 脚本中生成默认值,而不是在迁移代码中。
我最喜欢的是使用属性:https : //stackoverflow.com/a/34894274/132942
[SqlDefaultValue(DefaultValue = "getutcdate()")]
public DateTime CreatedDateUtc { get; set; }
Run Code Online (Sandbox Code Playgroud)
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class SqlDefaultValueAttribute : Attribute
{
public string DefaultValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
modelBuilder.Conventions.Add( new AttributeToColumnAnnotationConvention<SqlDefaultValueAttribute, string>("SqlDefaultValue", (p, attributes) => attributes.Single().DefaultValue));
Run Code Online (Sandbox Code Playgroud)
然后自定义SqlGenerator。但我不喜欢这样,因为我不知道在生成迁移时是否一切都如我所愿。
为此,我像这样修改了MigrationCodeGenerator:https : //stackoverflow.com/a/21024108
public class ExtendedMigrationCodeGenerator : MigrationCodeGenerator
{
public override ScaffoldedMigration Generate(string migrationId, IEnumerable<MigrationOperation> operations, string sourceModel, string targetModel, string @namespace, string className)
{
foreach (MigrationOperation …Run Code Online (Sandbox Code Playgroud) entity-framework default-value ef-code-first entity-framework-6 entity-framework-migrations
我的模型中有列CreatedAt和UpdatedAt列User.
User.cs
public string Name { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
Run Code Online (Sandbox Code Playgroud)
需求
SaveChanges()的用户记录,CreatedAt并UpdatedAt应自动保存如:DateTime.UtcNowUser记录时,只有UpdatedAt列应该更新到当前日期时间.OnModelCreating().latest从数据库和其他地方查找记录.code first迁移方法MySql.Data,MySql.Data.Entity.EF6.UPDATE
我添加了BaseEntity.cs模型
public abstract class BaseEntity
{
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; …Run Code Online (Sandbox Code Playgroud)