相关疑难解决方法(0)

将DateTime属性的默认值设置为System.ComponentModel内的DateTime.Now默认值Attrbute

有没有人知道如何使用System.ComponentModel DefaultValue属性为DateTime属性指定默认值?

例如我试试这个:

[DefaultValue(typeof(DateTime),DateTime.Now.ToString("yyyy-MM-dd"))]
public DateTime DateCreated { get; set; }
Run Code Online (Sandbox Code Playgroud)

并且它期望值是一个恒定的表达式.

这是在使用ASP.NET动态数据的上下文中.我不想构建DateCreated列,但只是提供DateTime.Now,如果它不存在.我使用实体框架作为我的数据层

干杯,

安德鲁

c# asp.net entity-framework asp.net-core-2.1 ef-core-2.1

87
推荐指数
9
解决办法
14万
查看次数

代码优先迁移:如何为新属性设置默认值?

我使用EF6 report在我的数据库中存储该类的实例.数据库已包含数据.说我想添加一个属性report,

public class report {
    // ... some previous properties

    // ... new property:
    public string newProperty{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我转到包管理器控制台并执行

add-migration Report-added-newProperty
update-database
Run Code Online (Sandbox Code Playgroud)

我将在'/ Migrations'文件夹中获取一个文件,newProperty向表中添加一列.这很好用.但是,对于数据库中较旧的条目,该值newProperty现在为空字符串.但我希望它是,例如,"老".

所以我的问题是:如何在迁移脚本(或其他地方)中为新属性(任何类型)设置默认值?

.net c# entity-framework ef-migrations entity-framework-6

37
推荐指数
3
解决办法
2万
查看次数

使用默认值在模型上注释属性

我创建了一个EF4.1代码优先模型(可能重要也可能不重要),我正在尝试获取我的创建脚手架模板的默认值.我的模型看起来像:

class Person {
    [DefaultValue (18)]
    public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后我的Create视图看起来像:

<div class="editor-label">
    @Html.LabelFor(model => model.Age)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Age)
    @Html.ValidationMessageFor(model => model.Age)
</div>
Run Code Online (Sandbox Code Playgroud)

我希望在运行时,EditorFor会用"18"预先填充文本框,但它没有这样的东西.我误解了DefaultValue属性的用途,还是我应该做的其他事情?

注意:我不想new { Value = "18" }在EditorFor方法上使用覆盖,它似乎打破了DRY.

entity-framework-4 data-annotations asp.net-mvc-3

26
推荐指数
2
解决办法
5万
查看次数

如何在Code First模型中的布尔值上设置默认值?

我有一个现有的表/模型,我想删除一个新的布尔列.该表已有数百行数据,我无法触及现有数据.但是..这个列不可为空,所以我需要为true当前存在的所有行提供默认值.

public class Revision
{
    ...
    public Boolean IsReleased { get; set; }
    ....
}
Run Code Online (Sandbox Code Playgroud)

重要:

(这是在OP,但人们似乎错过了.)

使用迁移更新数据库时,接收此新列的所有现有行必须将其值设置为True.

c# entity-framework ef-code-first

19
推荐指数
3
解决办法
3万
查看次数

实体框架代码第一个CTP4默认列值?

我一直在寻找使用Entity Framework CTP4的Code First,您可以使用ModelBuilder来构建表列.有没有办法使用ModelBuilder或其他一些机制为数据库中的列设置默认值?

谢谢!

c# entity entity-framework-4 ctp4

12
推荐指数
2
解决办法
7825
查看次数

如何自动填充CreatedDate和ModifiedDate?

我正在学习ASP.NET核心MVC,我的模型是

namespace Joukyuu.Models
{
    public class Passage
    {
        public int PassageId { get; set; }
        public string Contents { get; set; }


        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

Passage表用于保存我写的段落.

脚本

  • Create视图只有一个字段Contents来输入一个段落. CreatedDate并且ModifiedDate必须由服务器自动设置为相等(使用UTC格式).

  • Edit视图只有一个字段Contents来编辑一个段落.ModifiedDate必须由服务器自动设置.

根据上述场景,我必须将哪些属性附加到CreatedDateModifiedDate属性以使它们由服务器自动填充?

c# entity-framework asp.net-core-mvc

12
推荐指数
3
解决办法
1万
查看次数

Entity Framework 6 Code first 覆盖 MigrationCodeGenerator 的默认值

我发现的所有关于声明默认值的方法都是在 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)

在上下文的“OnModelCreating”中添加

modelBuilder.Conventions.Add( new AttributeToColumnAnnotationConvention<SqlDefaultValueAttribute, string>("SqlDefaultValue", (p, attributes) => attributes.Single().DefaultValue));
Run Code Online (Sandbox Code Playgroud)

然后自定义SqlGenerator。但我不喜欢这样,因为我不知道在生成迁移时是否一切都如我所愿。

为此,我像这样修改了MigrationCodeGeneratorhttps : //stackoverflow.com/a/21024108

在自定义 MigrationCodeGenerator

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

5
推荐指数
1
解决办法
464
查看次数

什么是等效于 modelBuilder 的 HasDefaultValueSql 的 Entity Framework Core 属性?

我想使用注释为 Entity Framework Core 中的属性设置默认值。问题是数据库没有设置默认值,因此该值没有传递到数据库层。

我想做一些类似于modelBuilder's 的事情HasDefaultValueSql

[DefaultValue("400")]
public int LengthInMeters {get; set;}
Run Code Online (Sandbox Code Playgroud)

您如何将以下代码转换为属性?

modelBuilder.Entity<Patient>().Property(c => c.LengthInMeters).HasDefaultValueSql("400");
Run Code Online (Sandbox Code Playgroud)

单独使用默认值是行不通的。我想单独使用属性而不必弄乱迁移。

问题:我已经尝试过使用 EF 的其他方法,但 Entity Framework Core 没有一些项目。如modelBuilder.Conventions也不AttributeToColumnAnnotationConvention也不CSharpMigrationCodeGenerator也不modelBuilder.Properties()

c# attributes custom-data-attribute entity-framework-core

5
推荐指数
1
解决办法
1486
查看次数

EF 6 Codefirst-使用Fluent API为基类中定义的属性设置默认值

我有一个具有审计属性的基类,例如

public abstract class BaseModel
{
    [Column(Order = 1)] 
    public long Id { get; set; }
    public long CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public long ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public bool IsActive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我所有的poco课程都来自于该课程。

我试图将默认值设置为IsActive属性。我不热衷于使用批注,因此如果我能使用流利的API来解决这个问题,我会一直徘徊。

我试过了,但是没有用。好像它创建了一个名为BaseModel的新表

modelBuilder.Entity<BaseModel>()
    .Property(p => p.IsActive)
    .HasColumnAnnotation("DefaultValue", true);
Run Code Online (Sandbox Code Playgroud)

有人可以在这里建议一种方法吗?

entity-framework entity-framework-6 ef-fluent-api

2
推荐指数
1
解决办法
2852
查看次数

在实体优先迁移中为新 Guid 列添加默认值

我正在为我的项目使用实体代码首次迁移。我已经启动并运行了系统。但是,我需要添加一个新的 Guid 列,它是一个外键。在尝试更新数据库时,我收到以下错误:

ALTER TABLE 语句与 FOREIGN KEY 约束“FK_dbo.Categories_dbo.aspnet_Roles_RoleId”冲突。冲突发生在数据库“HelpDesk”、表“dbo.aspnet_Roles”、“RoleId”列中。

所以我做了一些研究,发现Entity Framework 6 Code first Default value。但是,我无法弄清楚如何让它为 Guid 设置默认值。这是我试过的代码:

这是迁移:

public override void Up()
{

     AddColumn("dbo.Categories", "RoleId", c => c.Guid(nullable: false, defaultValue: "4468ACB7-AD6F-471E-95CF-615115EA3A76"));
     CreateIndex("dbo.Categories", "RoleId");
     AddForeignKey("dbo.Categories", "RoleId", "dbo.aspnet_Roles", "RoleId");
}

public override void Down()
{
     DropForeignKey("dbo.Categories", "RoleId", "dbo.aspnet_Roles");
     DropIndex("dbo.Categories", new[] { "RoleId" });
     DropColumn("dbo.Categories", "RoleId");
}
Run Code Online (Sandbox Code Playgroud)

如果我切换到此代码,我能够摆脱所有构建错误(但如果我运行 Update-database,仍然会出现 Alter Table 错误:

AddColumn("dbo.Categories", "RoleId", c => c.Guid(nullable: false, identity: false, defaultValue: null));
Run Code Online (Sandbox Code Playgroud)

如何将其转换为添加特定 Guid 作为默认值?

migration asp.net-mvc ef-code-first

1
推荐指数
2
解决办法
3517
查看次数