Answer the question
In order to leave comments, you need to log in
Convert DateTime to DateTime?
Model
public class Author
{
[Display(Name = "Author")] //Set names on Views
public Int32 Id { get; set; }
[Display(Name = "Author's name")] //Set names on Views
public String Name { get; set; }
[DataType(DataType.Date)] //Only date in this field
public DateTime Birthday { get; set; }
[DataType(DataType.Date)] //Only date in this field
public DateTime Death { get; set; }
public IEnumerable<Book> Books { get; set; }
}
// POST: api/AuthorsApi
[ResponseType(typeof(Author))]
public IHttpActionResult PostAuthor(Author author)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Это добавил, но не помогло
author.Birthday = author.Birthday.Date;
author.Death = author.Death.Date;
db.Authors.Add(author);
db.SaveChanges(); // ************** ТУТ ОШИБКА *****************
return CreatedAtRoute("DefaultApi", new { id = author.Id }, author);
}
CREATE TABLE [dbo].[Authors] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[Birthday] DATETIME NOT NULL,
[Death] DATETIME NOT NULL,
CONSTRAINT [PK_dbo.Authors] PRIMARY KEY CLUSTERED ([Id] ASC)
);
User-Agent: Fiddler
Host: localhost:62498
Content-Type: application/json
Content-Length: 69
{"id":"0", name:"aka", "Birthday":"01.02.1234", "death":"12.11.1098"}
SqlException: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
Answer the question
In order to leave comments, you need to log in
you most likely have a value / I author.Birthday; author.Death;
less than 01/01/1753 another
link
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question