尝试更新数据库记录时收到以下异常,即使我将正确的日期时间值传递给控制器。
System.ArgumentException:正在使用的SQL Server版本不支持数据类型'datetime2'
其他stackoverflow主题建议尚未设置日期时间或编辑edmx文件,在这种情况下,该文件不存在。我在Visual Studio 2015中使用localdb上下文测试了该应用程序,但在尝试连接到SQL Server 2005 DbContext时只收到错误。
是否由于与SQL Server 2005不兼容导致此异常,或者是否存在我缺少的解决方法?
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");
}
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); }
}
}
}
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; }
}
}
答案 0 :(得分:5)
EF Core不支持SQL Server 2005,但是支持2008和转发
https://docs.microsoft.com/en-us/ef/core/providers/sql-server/