Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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 Seed方法在DateTime上插入Null?_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 为什么EF Seed方法在DateTime上插入Null?

C# 为什么EF Seed方法在DateTime上插入Null?,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我试图在我的代码第一个EF6项目中实现AddOrUpdate方法。我收到一个错误,指出属性AdmCreatedDate中不允许使用null。下面是错误: 运行种子法。System.Data.Entity.Infrastructure.DbUpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.Entity.Core.UpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.SqlClient.S

我试图在我的代码第一个EF6项目中实现AddOrUpdate方法。我收到一个错误,指出属性AdmCreatedDate中不允许使用null。下面是错误:

运行种子法。System.Data.Entity.Infrastructure.DbUpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.Entity.Core.UpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.SqlClient.SqlException:无法将值NULL插入表“TPPX.dbo.ExceptionCalculationConfig”的列“AdmCreatedDate”;列不允许空值。插入失败。声明已终止

这是我的密码。。。 我有一个名为ExcemptionCalculationConfig的模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TPPX.Domain.ExemptionModels.Entities.Lookups;

namespace TPPX.Domain.ExemptionModels
{
    [Table("ExemptionCalculationConfig")]
    public class ExemptionCalculationConfig : TPPXBaseEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Priority { get; set; }

        [ForeignKey("ExemptionApplyLevel")]
        public string ApplyAtLevelCode { get; set; }

        public virtual ExemptionApplyLevel ExemptionApplyLevel { get; set; }

        [ForeignKey("ExemptionApplyType")]
        public string ApplyByValueOrPercentageCode { get; set; }

        public virtual ExemptionApplyType ExemptionApplyType { get; set; }
        public bool IsProratable { get; set; }
    }
}
如您所见,此类继承了TPPXBaseEntity:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace TPPX.Domain
{
    public class TPPXBaseEntity
    {
        [DefaultValue(true)]
        public bool IsActive { get; set; }

        public DateTime  AdmCreatedDate { get; set; }

        [StringLength(50)]
        public string AdmCreatedUser { get; set; }

        [StringLength(255)]
        public string AdmCreatedUserFullName { get; set; }
        public DateTime? AdmModifiedDate { get; set; }

        [StringLength(50)]
        public string AdmModifiedUser { get; set; }

        [StringLength(255)]
        public string AdmModifiedUserFullName { get; set; }

    }
}
然后,我的迁移文件夹中有Configuration.cs文件: 我已经尝试添加一个对象列表来保存在DB中,并且只添加了一个实例。此外,我还显式尝试创建名为seedCreatedDate的DateTime对象,使用DateTime.Today甚至DateTime.ParseExact()作为AdmCreatedDate的值,但没有成功:(

使用System.Collections.Generic;
使用System.Web;
使用PA.Web.Configuration.WebConfigurationManager;
使用TPPX.Domain.ExemptionModels;
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.Linq;
命名空间TPPX.DAL.Migrations.TPPXDBContext
{
内部密封类配置:DBMigOptionsConfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
MigrationsDirectory=@“Migrations\TPPXDBContext”;
}
受保护的覆盖无效种子(TPPX.DAL.dbcontext.TPPXDBContext)
{
//迁移到最新版本后将调用此方法。
//您可以使用DbSet.AddOrUpdate()助手扩展方法
//避免创建重复的种子数据。
//var seedCreatedDate=新日期时间(2018,07,26);
//var exemptionCalculationConfigs=新列表
//{
//新的ExcemptionCalculationConfig
//    {
//AdmCreatedDate=DateTime.Today,
//AdmCreatedUser=“carcruz”,
//AdmCreatedUserFullName=“克鲁兹,卡洛斯(宾夕法尼亚州)”,
//优先级=1,
//IsProratable=false,
//ApplyAtLevelCode=“A”,
//ApplyValue或PercentageCode=“P”,
//IsActive=true
//    },
//新的ExcemptionCalculationConfig
//    {
//AdmCreatedDate=DateTime.Today,
//AdmCreatedUser=“carcruz”,
//AdmCreatedUserFullName=“克鲁兹,卡洛斯(宾夕法尼亚州)”,
//优先级=2,
//IsProratable=false,
//ApplyAtLevelCode=“V”,
//ApplyValue或PercentageCode=“V”,
//IsActive=true
//    }
//};
//豁免计算配置s.ForEach(
//x=>context.ExemptionCalculationConfigs.AddOrUpdate(e=>e.Priority,x));
context.ExemptionCalculationConfigs.AddOrUpdate(
e=>e.优先级,
新的ExcemptionCalculationConfig
{
AdmCreatedDate=DateTime.Now,
AdmCreatedUser=“carcruz”,
AdmCreatedUserFullName=“克鲁兹,卡洛斯(宾夕法尼亚州)”,
优先级=1,
IsProratable=false,
ApplyAtLevelCode=“A”,
ApplyValue或PercentageCode=“P”,
IsActive=true,
AdmModifiedDate=null,
AdmModifiedUser=null,
AdmModifiedUserFullName=null
});
SaveChanges();
}
}
}
我看到其他人也有同样的错误,他们没有明确地说他们的主键不应该是标识列,但这不是我的情况。我遗漏了什么?

好的,伙计们

我算出来了

感谢伊凡斯托夫,他指出了计算机和身份属性

我的DbContext中确实有以下行:

modelBuilder.Properties<DateTime>().Where(x => x.Name == "AdmCreatedDate")
.Configure(c => c.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed));
modelBuilder.Properties()。其中(x=>x.Name==“AdmCreatedDate”)
.Configure(c=>c.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed));
出于某种原因,当Seed方法尝试运行并且对象具有计算属性时,EF不喜欢它(因为它甚至不影响DB。EF只在代码中处理它。在SQL Server中检查DB后,我注意到该列实际上没有设置为计算列)老实说,我不喜欢这样,因为如果将来我们决定不使用EF,计算属性将丢失

总之,在Seed()方法中不能有具有计算属性的模型

现在,我们处理实际数据库中的计算列

很高兴知道的是,我在网上找不到英孚这样做的原因。如果有人知道,请告诉我


希望这有帮助:)

[DatabaseGenerated(DatabaseGenerateOptions.Identity)]
属性添加到AdmCreatedDate属性。数据库中的该列可能应该使用默认值GETDATE()或GETUTCDATE()创建,因此数据库生成值,而不是应用程序。看起来
AdmCreatedDate
已通过fluent API配置为
DatabaseGeneratedOption.Identity
,在这种情况下,EF在执行
INSERT
时不会发送值。但同时数据库表列未设置
默认值不允许出现异常。@RobertMcKee IMHO异常消息
modelBuilder.Properties<DateTime>().Where(x => x.Name == "AdmCreatedDate")
.Configure(c => c.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed));