如何强制 entity framework 使用 datetime 而不是 datetime2 进行存储过程调用?

How to force entity framework to use datetime instead of datetime2 for stored procedure call?

我的存储过程中有一个 datetime 输入。

当使用数据库优先方法构建 edmx 时,我们有一个

((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("spInsert", ... , payDateParameter);

payDateParameter 定义为:

var payDateParameter = payDate.HasValue ?
    new ObjectParameter("PayDate", payDate.Value) :
    new ObjectParameter("PayDate", typeof(System.DateTime));

其中 payDateDateTime?.

异常:

The version of SQL Server in use does not support datatype 'datetime2'.

我知道 datatime 数据类型有范围限制。所以加个最小值再试一次

var payDateParameter = payDate.HasValue ?
    new ObjectParameter("PayDate", payDate.Value) :
    new ObjectParameter("PayDate", new DateTime(1753,1,1));

还是一样的异常。

有没有办法强制存储过程调用使用我的 payDateParameter 作为 datatime 类型而不是 datetime2

您需要在文本编辑器中打开 edmx 并更改以下内容

<Schema Namespace="CPTTModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store=....

The important thing to look at is the “ProviderManifestToken” parameter. According to Microsoft, this parameter helps the entity model function while not connected to the database, I think it also helps best structure the queries it generates against the target database. The tricky thing is that every time you update the data model, even to move an object around in the display editor, this value is updated and saved to the database it is connected to.