我已经通过IdentityUser
类扩展了ApplicationUser
,该类具有几个自定义字段,以匹配我在SQL Server DB中拥有的AspNetUsers
表。其中有两个在SQL中定义为,例如:MyDate datetime2 null
。在我的ApplicationUser
类中,它将是:datetime? MyDate;
。
现在,我可以将其设置为null的唯一方法是使用与Entity Framework分开的T-SQL命令,如果为null,则我的应用程序会引发从数据库读取它的异常。
我确实承认,在我对其进行更新以匹配数据库之前,它在datetime MyDate
中被错误地声明为ApplicationUser
。这是我看到的问题的原因吗?如果是这样,如何获取我的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)
{
}
}
答案 0 :(得分:2)
DateTime是Struct
,因此是value type。这意味着不能将其设置为null
。
您可以在可为空的运算符的帮助下解决您的问题。 在类型之后使用问号(?)或使用通用样式Nullable。
Nullable <DateTime> MyDateUtc;
或
DateTime? MyDateUtc;
您应该检查您的DbContext
班级,以防万一您需要设置班级的属性。更多信息here