Rune Hansen
Rune Hansen

Reputation: 1024

How to ignore a particular field from an Entity model upon insert?

We have a field in our SQL Server database table which is autogenerated by SQL Server, the field is called CreatedTime.

We have mapped the whole database table to our datamodel in Entity Framework, thus also the field CreatedTime.

When we insert a new row in the database, via Entity Framework, we thus do not provide any value for CreatedTime.

This causes the insert to fail with the error:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

So the question is: Is there is a way to to exclude a particular field in the Entity datamodel in the Entity Framework insert statement? So that we will not get the above error?

We would like to keep the field CreatedTime in the Entity model, because we might want to access it later.

Upvotes: 6

Views: 9942

Answers (5)

Jason Shehane
Jason Shehane

Reputation: 423

If using Fluent API:

using System.ComponentModel.DataAnnotations.Schema;

this.Property(t => t.CreatedTime)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

If using Annotations

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public System.DateTime CreatedTime { get; set; }

Upvotes: 9

Rune Hansen
Rune Hansen

Reputation: 1024

I found a simple solution to the problem on this thread:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7db14342-b259-4973-ac09-93e183ae48bb

There Fernando Soto writes:

"If you go to the EDM designer click on the field in the table that is auto-generated by the database, right click on it and select Properties and look at the properties windows click on StoreGeneratedPattern and set its value to Computed, I believe it will give you what you are looking for."

The above solution was super quick and easy and it seems to work.

Also thank you for your contributions guys, but the above solution seems to do the job.

Upvotes: 4

RoelF
RoelF

Reputation: 7573

there are two things you can do:

  1. If you have access to the database, check if the field has a default value. If it doesn't you can set it to GETDATE(), and the field should be set correctly, and you don't have to add/update it through Entity Framework.

  2. If you don't have access to the database, or don't want to make any changes there, you can alter the behavior of the Entity Data Model to automatically set the date. Simply extend your ObjectContext model.

    public partial class MyEntities 
    {
        public override int SaveChanges() 
        {
            var entityChangeSet = ChangeTracker.Entries<SomeEntity>();
            if (entityChangeSet != null)
            {
                foreach (DbEntityEntry<SomeEntity> entry in entityChangeSet )
                {
                    switch (entry.State)
                    {
                        case EntityState.Modified:
                            entry.Entity.LastModifiedDate = DateTime.UtcNow;
                            break;
                        case EntityState.Added:
                            entry.Entity.CreatedDate = DateTime.UtcNow;
                            break;
                    }
                }
            }
            return base.SaveChanges();
        }
    }
    

This way you don't have to add any information for those fields when you add or update an item, the model will do it for you. If you have multiple entities which need this behavior, you can create an interface and make the Entity classes inherit that:

public interface IHaveCreatedDate {
    DateTime CreatedDate { get; set; }
}

public partial class MyEntity : IHaveCreatedDate {
    //MyEntity already implements this!
}

Then all you need to do is change the call to the ChangeTracker:

var entityChangeSet = ChangeTracker.Entries<IHaveCreatedDate>();

Upvotes: 1

Rui Jarimba
Rui Jarimba

Reputation: 18084

Is CreatedTime nullable?

One possible workaround - if CreatedTime is NOT nullable:

DateTime sqlServerMinDateTime = new DateTime(1753, 1, 1, 12, 0, 1, 0);

if(myEntity.CreatedTime < sqlServerMinDateTime) 
{
    myEntity.CreatedTime = sqlServerMinDateTime;
}

// do insert here
// ....

One possible workaround - if CreatedTime is nullable:

DateTime sqlServerMinDateTime = new DateTime(1753, 1, 1, 12, 0, 1, 0);

if(myEntity.CreatedTime < sqlServerMinDateTime) 
{
    myEntity.CreatedTime = null;
}

// do insert here
// ....

Upvotes: 0

Related Questions