22

I have an ASP.NET MVC application where I am editing an existing database to update a paticular field, DateTime. My database has 4 fields, two of which are DateCreated and DateModified. When I try to update the field, I want to keep DateCreated time the same, no reason to update the date it was created, and I change the DateModified time to the current time using DateTime.Now

Here is the given code just in-case I am doing something wrong. This is my first time using ASP.NET MVC so go gentle. I have seen other answers where Context is called, but I can't find any reference to it. When I run the application I receive the error message in the title and the contractEntity.SaveChanges() is in red.

public ActionResult Edit(Contract editContract) {
var contract = (from c in contractEntity.Contracts where c.Id == editContract.Id select c).First();
if (!ModelState.IsValid)
    return View(contract);
// editContract.DateCreated = contract.DateCreated;
// editContract.DateModified = DateTime.Now;
  contractEntity.ApplyCurrentValues(contract.EntityKey.EntitySetName, editContract);
  contractEntity.SaveChanges();
  return RedirectToAction("Index");
}

Please, any help is appreciated. Thanks.

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
  • I believe that since this post was written, modifying the edmx by hand is not the answer. The one posted in 2011 is a more likely candidate by Jamiegs. – ΩmegaMan Mar 05 '15 at 02:36

5 Answers5

38

For me, I had the same issue, but the problem was that by default when I saved my Model, an invalid DateTime was being created. I had a field CreatedOn and had not set it to anything, which meant the value was 01/01/0001 which was an invalid DateTime for SQL. Setting it got rid of the problem for me.

Colin Asquith
  • 653
  • 1
  • 6
  • 13
  • 2
    I wonder why still EF 4.3 Code-First creates a datetime field for an SQL Server 2008 R2 Database instead of datetime2. – IsmailS May 27 '12 at 11:06
19

After reading this website I found to open the .edmx file for my database and change:

<...Provider="System.Data.SqlClient" ProviderManifestToken="2008".../>

to

<...Provider="System.Data.SqlClient" ProviderManifestToken="2005".../>

Is this acceptable or is there a better approach to fixing that error?

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
  • http://ifunky.net/post/Type-datetime2-is-not-a-defined-system-type-Entity-Framework.aspx Sorry, would have helped if I had posted. – Anthony Forloney Nov 05 '09 at 05:35
  • 5
    This change stops the EF from using DATETIME2 type at all. Is that the correct solution for your app? Only you can say that. If you intend to use DATETIME2, plan around that. – Craig Stuntz Nov 05 '09 at 18:37
  • This is the correct solution for my particular app, but do you know of a way around it? I do not know why the error occured since the field i was changing was DateTime not DateTime2 – Anthony Forloney Nov 05 '09 at 23:18
  • 3
    This is a really annoying issue. I'm using VS2010, EF 4.0, and the databases are running on SQL Server 2008. I can't modify the db to use datetime2 fields. It would be much better if EF was explicit about the property type in the edmx, because right now it says 'DateTime', but it's converting that to datetime2, not datetime. – marcel_g Jul 20 '11 at 15:16
11

You can also edit the model (or in EF in the .edmx) and set the StoreGeneratedPattern to Computed for the field in question. That will cause it not to save that to that field, because it gets calculated by the SQL server.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
jamiegs
  • 1,761
  • 2
  • 15
  • 23
  • This way works best in this case. Just be aware that it *only* works if you have a default specified in the database. Since the question is about DateCreated and DateModified, these will probably be set up as `DEFAULT GETDATE()` – CrazyPyro Mar 28 '14 at 22:10
7

I have the same error and i discovered that if you have a SQL 2008 DB with a DateTime Nullable field, DON'T explicit assign Null (Nothing) to the field in your code, this will cause this error, or you can change all DateTime fields to DateTime2.

InterWAS
  • 183
  • 1
  • 2
  • 7
3

Just use a DateTime nullable such DateTime? in your domain entity property if your column in your database can be nullable. This will make it work.

Rushino
  • 9,415
  • 16
  • 53
  • 93