【发布时间】:2015-10-18 19:19:39
【问题描述】:
我已经在身份用户表中创建了新的自定义字段,因为 asp.net 身份没有“上次登录日期”、“注册日期”和“个人资料更新日期”等所有字段。
在帐户注册期间,我收到有关 datetime2 超出范围的错误消息。所有 3 个日期字段都是日期时间类型并设置为“允许 Null”。在这个过程中,我只需要设置“注册日期”的日期,其余的应该保持不变,但我不知道该怎么做?
此错误来自“上次登录日期”和“个人资料更新日期”。
创建用户函数
namespace Web_WebApp.Account
{
public partial class Register : Page
{
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = UserName.Text, Email = Email.Text, FirstName = FirstName.Text, MiddleName = MiddleName.Text, LastName = LastName.Text, RegistrationDate = DateTime.Now };
IdentityResult result = manager.Create(user, Password.Text );
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
//string code = manager.GenerateEmailConfirmationToken(user.Id);
//string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
//manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
signInManager.SignIn( user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
}
}
迁移文件
namespace Web_WebApp.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class NewDateFields : DbMigration
{
public override void Up()
{
AddColumn("dbo.User", "LastLoginDate", c => c.DateTime(nullable: true));
AddColumn("dbo.User", "RegistrationDate", c => c.DateTime(nullable: true));
AddColumn("dbo.User", "ProfileUpdateDate", c => c.DateTime(nullable: true));
}
public override void Down()
{
DropColumn("dbo.User", "ProfileUpdateDate");
DropColumn("dbo.User", "RegistrationDate");
DropColumn("dbo.User", "LastLoginDate");
}
}
}
错误信息
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.
感谢您为解决我的问题所做的努力。
【问题讨论】:
标签: c# asp.net datetime webforms asp.net-identity