Hi can you please help on this exception. I am putting together a C# asp.net MVC site. One of the models is SystemUser as shown here
public class SystemUser : DefaultModel
{
public int Id { get; set; }
[Required]
[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
[Column("FirstName")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Full Name")]
public string Name
{
get
{
return string.Format("{0},{1}", this.FirstName, this.LastName);
}
}
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Hire Date")]
public DateTime HireDate { get; set; }
[Required]
public int inactive { get; set; }
public virtual ApplicationUser User { get; set; }
public string ApplicationUserId { get; set; }
}
The SQL DB table def looks like this
CREATE TABLE [dbo].[SystemUser] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[LastName] NVARCHAR (50) NOT NULL,
[FirstName] NVARCHAR (50) NOT NULL,
[HireDate] DATETIME NULL,
[inactive] INT NOT NULL,
[ApplicationUserId] NVARCHAR (128) NULL,
[CreatedDate] DATETIME DEFAULT (getdate()) NOT NULL,
[UpdatedDate] DATETIME DEFAULT (getdate()) NOT NULL,
[UpdatedUserId] INT DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_dbo.SystemUser] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.Person_dbo.AspNetUsers_ApplicationUserId] FOREIGN KEY ([ApplicationUserId]) REFERENCES [dbo].[AspNetUsers] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_ApplicationUserId]
ON [dbo].[SystemUser]([ApplicationUserId] ASC);
When using the registration page on the site, the credentials are processed in the default AccountController. I have modified this to also create a SystemUser by adding four lines of code in the Register method as shown here.
if (result.Succeeded)
{
var db = new ApplicationDbContext();
var systemUser = new SystemUser { FirstName = model.FirstName, LastName = model.LastName, inactive = 0, ApplicationUserId = user.Id, HireDate = DateTime.Now };
db.SystemUsers.Add(systemUser);
db.SaveChanges();
When I run process the registration, the record is inserted into the AspNetUsers table ok, then I receive an exception on the db.SaveChages(); call as follows,
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
Line 160: var systemUser = new SystemUser { FirstName = model.FirstName, LastName = model.LastName, inactive = 0, ApplicationUserId = user.Id, HireDate = DateTime.Now };
Line 161: db.SystemUsers.Add(systemUser);
Line 162: db.SaveChanges();
Line 163:
Line 164: await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
Thanks in advance for any help on this.
Aucun commentaire:
Enregistrer un commentaire