0

I am using Entity Framework 6 to connect to the database. And I used one DateTime Entity but it gives error at the time of database initialization:

1)

Validation Failed For one or more entities ,See "EntityValidationErrors" for more Detail

2)

An Exception occured while initializing database.

IList<tblSystemSettings> defaulttblSystemSettings = newList<tblSystemSettings>();

defaulttblSystemSettings.Add(new tblSystemSettings() 
{ 
   LastModify = DateTime.Now
});

foreach (tblSystemSettings systemsettings in defaulttblSystemSettings)
{
   context.tblSystemSettings.Add(systemsettings);
}
context.SaveChanges();


 public class tblSystemSettings
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int SystemSettingsId { get; set; }

    [DataType(DataType.Date)]//daivshala
    public DateTime LastModify { get; set; }

}
Daivshala
  • 11
  • 3
  • post the `create table script` for the table you're saving to – jazb May 06 '19 at 05:10
  • Did code every work? Did the database table definitions change? Did version of Net Library change? Entity creates c# classes from the database and then creates a mapping between the classes and the database. Error indicates there is an issue in the mapping. Not sure why the mapping would of changed to create there errors. – jdweng May 06 '19 at 05:12
  • But ,Not giving error every time, its occured only sometime – Daivshala May 06 '19 at 05:17
  • Please give us the inner exception of entityvalidationerror – programmer444 May 06 '19 at 05:23
  • Inner exception is Object Not set instance of an object – Daivshala May 06 '19 at 05:34
  • I think programmer444 wanted you to post the EntityValidationErrors that you have in the Exception object. it is above the inner exception and you need to drill down to find what was the problem. – Yair I May 06 '19 at 09:02
  • https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert – Steve Greene May 06 '19 at 13:11

0 Answers0