Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF 2.1核心IEntityTypeConfiguration添加默认值_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# EF 2.1核心IEntityTypeConfiguration添加默认值

C# EF 2.1核心IEntityTypeConfiguration添加默认值,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我正在使用EF Core 2.1创建一个数据库。我可以设置列的默认值,如 public class SchoolContext : DbContext { .... protected override void OnModelCreating(ModelBuilder modelBuilder) { .... modelBuilder.Entity<Student>().Property(s => s.LastLoginDat

我正在使用EF Core 2.1创建一个数据库。我可以设置列的默认值,如

public class SchoolContext : DbContext
{
    ....
    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        ....
         modelBuilder.Entity<Student>().Property(s => s.LastLoginDateTime).HasDefaultValueSql("SYSUTCDATETIME()");
    }
    ....
}

Configure
方法的
builder
参数类型是
EntityTypeBuilder
,并且与
ModelBuilder.Entity
方法返回的参数类型完全相同

因此,在使用
ientitypeconfiguration
时,您应该直接使用
builder
(w/o
Entity()
调用,该调用用于
ModelBuilder
):

公共类StudentConfig:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
属性(s=>s.LastLoginDateTime).HasDefaultValueSql(“SYSUTCDATETIME()”);
}
}
顺便说一句,
ModelBuilder.Entity()
方法具有带有
操作的重载
{
属性(s=>s.LastLoginDateTime).HasDefaultValueSql(“SYSUTCDATETIME()”);
});

更新:请注意,
HasDefaultValueSql
是程序集中的
RelationalPropertyBuilderExtensions
类中定义的扩展方法,因此请确保您的项目正在引用它。
Configure
方法的
builder
参数类型是
EntityTypeBuilder
,和
ModelBuilder.Entity
方法返回的完全相同

因此,在使用
ientitypeconfiguration
时,您应该直接使用
builder
(w/o
Entity()
调用,该调用用于
ModelBuilder
):

公共类StudentConfig:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
属性(s=>s.LastLoginDateTime).HasDefaultValueSql(“SYSUTCDATETIME()”);
}
}
顺便说一句,
ModelBuilder.Entity()
方法具有带有
操作的重载
{
属性(s=>s.LastLoginDateTime).HasDefaultValueSql(“SYSUTCDATETIME()”);
});

更新:请注意,
HasDefaultValueSql
是程序集的
RelationalPropertyBuilderExtensions
类中定义的扩展方法,因此请确保您的项目正在引用它。

Ivan,如果我理解正确,公共类StudentConfig:IEntityTypeConfiguration{…}前面提到的是正确的。但是,方法或函数列表中没有“HasDefaultValueSql”,因此出现生成错误CS1061“PropertyBuilder”不包含“HasDefaultValueSql”的定义,并且没有扩展方法“HasDefaultValueSql”接受类型为“PropertyBuilder”的第一个参数。是我误解了你的答案还是我在某些方面迷失了方向。你问题中的错误代码是
builder.Entity()
。使用
builder.Property(..)
后,您应该可以访问与
OnModelCreating
中相同的方法,包括
HasDefaultValueSql
。您仍然收到编译器错误吗?请注意,许多EF核心方法都是扩展方法,它们来自不同的程序集。对于这个特定的组件,请确保引用Microsoft.EntityFrameworkCore.Relationalassembly。虽然你所说的模型创建中的
OnModelCreating
中的代码也需要同样的功能,但这是我的错。您是对的,公共类StudentConfig中的builder.Entity().Property…应该是builder.Property…我正在检查不同版本的代码。问题所在的项目中缺少Microsoft.EntityFrameworkCore.Relational。我从NuGet加的。没有更多的编译器错误。非常感谢你!Ivan,如果我理解正确,前面提到的公共类StudentConfig:IEntityTypeConfiguration{…}中的代码是正确的。但是,方法或函数列表中没有“HasDefaultValueSql”,因此出现生成错误CS1061“PropertyBuilder”不包含“HasDefaultValueSql”的定义,并且没有扩展方法“HasDefaultValueSql”接受类型为“PropertyBuilder”的第一个参数。是我误解了你的答案还是我在某些方面迷失了方向。你问题中的错误代码是
builder.Entity()
。使用
builder.Property(..)
后,您应该可以访问与
OnModelCreating
中相同的方法,包括
HasDefaultValueSql
。您仍然收到编译器错误吗?请注意,许多EF核心方法都是扩展方法,它们来自不同的程序集。对于这个特定的组件,请确保引用Microsoft.EntityFrameworkCore.Relationalassembly。虽然你所说的模型创建中的
OnModelCreating
中的代码也需要同样的功能,但这是我的错。您是对的,公共类StudentConfig中的builder.Entity().Property…应该是builder.Property…我正在检查不同版本的代码。问题所在的项目中缺少Microsoft.EntityFrameworkCore.Relational。我从NuGet加的。没有更多的编译器错误。非常感谢你!
public class StudentConfig : IEntityTypeConfiguration<Student>
{
    public void Configure (EntityTypeBuilder<Student> builder)
    {
        ....
        // Error    CS1061  'PropertyBuilder<DateTime>' does
        // not contain a definition for 'HasDefaultValueSql' 
        // and no extension method 'HasDefaultValueSql' 
        // accepting a first argument of Type 'PropertyBuilder<DateTime>' 
        // could be found (are you missing a using directive
        // or an assembly reference?) 
        // Data C:\Users\Paul\source\repos\School\Data\EFClasses
        // \Configurations\StudentConfig.cs 22  Active

        builder.Entity<Student>().Property(s => s.RecIn).HasDefaultValueSql("SYSUTCDATETIME()");
        ....
    }
}
public class StudentConfig : IEntityTypeConfiguration<Student>
{
    public void Configure(EntityTypeBuilder<Student> builder)
    {
        builder.Property(s => s.LastLoginDateTime).HasDefaultValueSql("SYSUTCDATETIME()");
    }
}
modelBuilder.Entity<Student>(builder =>
{
    builder.Property(s => s.LastLoginDateTime).HasDefaultValueSql("SYSUTCDATETIME()");
});