插入时如何忽略实体模型中的特定字段?

问题描述:

我们在SQL Server数据库表中有一个由SQL Server自动生成的字段,该字段称为 CreatedTime

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

我们已经将整个数据库表映射到Entity Framework中的数据模型,因此也映射了 CreatedTime 字段。

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

当我们通过Entity Framework在数据库中插入新行时,因此不会为 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溢出。必须在1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间

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?

我们想在实体模型中保留字段 CreatedTime ,因为我们以后可能想访问它。

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

我在此线程上找到了解决该问题的简单方法:

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

那里有Fernando Soto写道:

There Fernando Soto writes:

如果您去EDM设计器,请单击数据库自动生成的表中的字段,单击它,选择Properties,然后选择
,然后在属性窗口中单击StoreGeneratedPattern,并将其值设置为Computed,我相信它将为您提供所需的东西。

"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.