Reputation: 99
I am trying to insert data from another database and this code was working until today. Now it doesn't anymore.
Here is the code:
var itemMember = Db.Members.FirstOrDefault(p => p.Id == CurrentMember.Id);
var itemSelectedName = Db.Topics.FirstOrDefault(p => p.Id == TopicId);
var itemMap = Db.Reeves.FirstOrDefault(p=> p.Member.Id == CurrentMember.Id);
var itemReeve = Db.Reeves.FirstOrDefault(p => p.Member.Id == CurrentMember.Id);
var item = new Problem
{
Name = input.Name,
Text = input.Info,
Topic = itemSelectedName,
WhoIs = itemMember,
Province = itemMap.Province,
District = itemMap.District,
Town = itemMap.Town,
Reeve=itemReeve,
Created=DateTime.Now,
Solved = false,
Read=false
};
Db.Problems.Add(item);
Db.SaveChanges();
There is nothing null in the code and it throws an error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
What should I do ?
Also this error shows up now:
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.
but as I said it was working till today :/
Here is view model:
[Display(Name = "Başlık"), Required, StringLength(50)]
public string Name { get; set; }
[Display(Name = "Bilgi")]
[UIHint("TinyMCE")]
public string Info { get; set; }
[Required(ErrorMessage = "Gerekli Alan")]
[Display(Name = "Konu")]
[UIHint("DropDownList")]
[AdditionalMetadata("DataController", "Problem")]
[AdditionalMetadata("DataAction", "ProblemDrop")]
public int? TopicId { get; set; }
Here is model
public class Problem
{
public int Id { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public short Sira { get; set; }
public DateTime Created { get; set; }
public virtual Topic Topic { get; set; }
public virtual Member WhoIs { get; set; }
public virtual Province Province { get; set; }
public virtual Town Town { get; set; }
public virtual District District { get; set; }
public virtual Reeve Reeve { get; set; }
public Boolean Solved { get; set; }
public DateTime SolvedDate { get; set; }
public virtual Member WhoSolved { get; set; }
public Boolean Read { get; set; }
}
Solved: Date is null could it be the problem ?
Upvotes: 3
Views: 20749
Reputation: 51
This worked in ASP.NET, when making the field nullable was not an option:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
}
Upvotes: 0
Reputation: 1
I received the same error after creating a new viewmodel. I changed my DateTime field(s) to nullable (DateTime?) in the viewmodel and it solved my problem.
Upvotes: 0
Reputation: 6696
You did not set a value for SolvedDate
, so the default value which is 01\01\01
will be sent to the database, which causes the error.
If you want to have SolvedDate
with null
value, you should define it as Nullable
date(DateTime?
) because DateTime
is a struct
and cannot be set to null
.
Upvotes: 4
Reputation: 3571
That error appears when you try to insert invalid date into datetime
field in DB. Check this field in Problem
model:
public DateTime SolvedDate { get; set; }
This should be made null
or initialized to some value.
Upvotes: 0