标识日期时间?字段不允许空



我用一个ApplicationUser类扩展了IdentityUser,该类具有多个自定义字段,以匹配我在SQL Server DB中的AspNetUsers表。其中有几个在SQL中定义为,例如:MyDate datetime2 null 。在我的ApplicationUser课上,它会是:datetime? MyDate;.

现在,我可以将其设置为 null 的唯一方法是使用独立于实体框架的 T-SQL 命令,如果它为 null,我的应用程序会引发从数据库读取它的异常。

承认在我更新它以匹配数据库之前,它在 ApplicationUser 中被错误地声明为 datetime MyDate 一段时间。这是我看到的问题的原因吗?如果是这样,如何使ApplicationUser与当前数据库架构匹配?

以下是例外情况:

01/10/19 17:14:47.034 ERROR 114 MyApp.Global - Exception occurred: 
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Data.ConstraintException: The 'MyDateUtc' property on 'ApplicationUser' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.DateTime'. 
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at lambda_method(Closure , Shaper )
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.<MoveNextAsync>d__4.MoveNext()

以下是ApplicationUser类(略有删减)和支持类:

public class ApplicationUser : IdentityUser<long, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
    public override long Id
    {
        get { return base.Id; }
        set { base.Id = value; }
    }
    public DateTime? MyDateUtc { get; set; }
    public ClaimsIdentity GenerateUserIdentity(UserManager<ApplicationUser, long> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationDbContext() : base("MyDbConnection")
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
public class CustomUserRole : IdentityUserRole<long> { }
public class CustomUserClaim : IdentityUserClaim<long> { }
public class CustomUserLogin : IdentityUserLogin<long> { }
public class CustomRole : IdentityRole<long, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, long, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public CustomUserStore(ApplicationDbContext context) : base(context)
    {
    }
}
public class CustomRoleStore : RoleStore<CustomRole, long, CustomUserRole>
{
    public CustomRoleStore(ApplicationDbContext context) : base(context)
    {
    }
}

DateTime 是一个Struct,因此是一种值类型。这意味着它不能设置为 null .

您可以在可为空运算符的帮助下解决问题。在类型后使用问号 (?) 或使用通用样式 Nullable。

Nullable <DateTime> MyDateUtc;

DateTime? MyDateUtc;

您应该检查您的DbContext类,以防您的类的属性被设置为必需。更多信息在这里

相关内容

  • 没有找到相关文章

最新更新