【发布时间】:2017-01-23 03:48:14
【问题描述】:
我在尝试更新数据库记录时收到以下异常,即使我将正确的日期时间值传递给控制器。
System.ArgumentException:使用的 SQL Server 版本不支持数据类型“datetime2”
其他 stackoverflow 主题建议未设置日期时间或编辑 edmx 文件,在这种情况下,该文件不存在。我在 Visual Studio 2015 中使用 localdb 上下文测试了应用程序,但在尝试连接到 SQL Server 2005 DbContext 时只收到错误消息。
此异常是由于与 SQL Server 2005 不兼容还是我缺少的解决方法?
控制器/SecureController/保存
public ActionResult save([FromForm]SubscriptionsViewModel newSubscription)
{
if (ModelState.IsValid)
{
var mySubscription = Mapper.Map<Subscription>(newSubscription);
_context.Entry(mySubscription).State = EntityState.Modified;
_context.SaveChanges();
_logger.LogInformation("saving to database");
return RedirectToAction("subscription", "secure");
}
return RedirectToAction("index", "secure");
}
ViewModels/SubscriptionsViewModel.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace XX.ViewModels
{
public class SubscriptionsViewModel
{
public int SubscriptionRef { get; set; }
public int MemberID { get; set; }
public string SubTypeID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public byte PrimaryPaysInd { get; set; }
[NotMapped]
[StringLength(15, MinimumLength = 4)]
public string searchParam { get; set; }
public SubscriptionsViewModel()
{
}
[NotMapped]
public bool PrimaryPaysIndBool
{
get { return PrimaryPaysInd > 0; }
set { PrimaryPaysInd = value ? Convert.ToByte(1) : Convert.ToByte(0); }
}
}
}
Models/Subscription.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace XX.Models
{
public class Subscription
{
[Key]
public int SubscriptionRef { get; set; }
public int MemberID { get; set; }
public string SubTypeID { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public byte PrimaryPaysInd { get; set; }
}
}
【问题讨论】:
-
数据类型
datetime2不在 SQL Server 2005 中。它是 2008 年及更高版本 -
我没有在我的
SubscriptionsViewModel类中指定datetime2;只有datetime。 -
这段代码在前吗?
-
是的,尽管没有迁移。
-
你能把
SubscriptionsViewModel代码也放上吗?
标签: entity-framework sql-server-2005 entity-framework-6 asp.net-core-mvc