Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:09:47+00:00 2026-06-02T18:09:47+00:00

For simplicity’s sake, let’s say I have a SQL Server CE table called Widgets

  • 0

For simplicity’s sake, let’s say I have a SQL Server CE table called Widgets:

create table [Widgets] (
  [Id] int not null primary key,
  [Created] datetime not null default getdate())

Inserting without specifying a value for the Created column works as expected:

insert into [Widgets] ([Id]) values (1)

After generating an Entity Framework model called Database, I write following code:

Database db = new Database();
db.Widgets.AddObject(new Widget { Id = 2 });
db.SaveChanges();

But it raises an exception: An overflow occurred while converting to datetime. I tracked this down to the fact that EF sees the Created column as a non-nullable DateTime, and so when a new Widget is constructed, its Created property is set to the default DateTime (0001-01-01 12:00:00), which is outside of the valid range for SQL Server CE.

So, how do I get the above sample of C# to work? I’d prefer not to change the database schema. And I know I could set the Created property to DateTime.Now in the Widget constructor, but it may take a few minutes from the time a Widget is constructed until its actually inserted into the database, which is that actual time I want to capture.

Update: I tried setting StoreGeneratedPattern to Identity and Computed, but it raises an exception: Server-generated keys and server-generated values are not supported by SQL Server Compact. Default values do work in SQL Server CE, so it must be a limitation of Entity Framework. (Apparently this is fixed in SQL Server CE 4.0.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:09:48+00:00Added an answer on June 2, 2026 at 6:09 pm

    Looks like DanM’s related question link pointed me in the right direction toward change tracking. The following code will get pretty close to the behavior I’m looking for:

    public partial class Database
    {
      public override int SaveChanges(SaveOptions options)
      {
        foreach(var w in this.ObjectStateManager
                             .GetObjectStateEntries(EntityState.Added)
                             .Where(e => e.Entity is Widget)
                             .Select(e => (Widget)e.Entity))
        {
          if(w.Created == default(DateTime))
            w.Created = DateTime.Now;
        }
    
        return base.SaveChanges(options);
      }
    }
    

    All of the Widgets’ Created values will be set to the current date and time. The only other issues I see is that the value is set according to the client’s clock, not the server’s. And if you’re inserting many rows at once, the timestamps will be a little ahead of what they would be with a normal insert.

    The best solution would allow SQL Server to do what it’s configured to do—set the value of the column if not provided. C’est la vie.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For simplicity's sake, let's say we have this rather contrived table: [ID] [Weekday] [Weather]
For the sake of simplicity, let's say that we have input strings with this
For simplicity let's say I have two classes: a class called Car and a
For the sake of simplicity, let's say I have a Person class in Python.
I have a view helper, let say (for simplicity's sake) def call_alert return alert
For simplicity let's say I have code similar to this: def testMethod(String txt) {
For the sake of simplicity say I have multiple Xtext DSLs that all describe
For the sake of simplicity, let's say my database has two tables, videos and
For the sake of simplicity, let's assume the following tables exist: Table 1 -
For the sake of simplicity, lets say I have a CustomerViewModel and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.