当尝试更新数据库记录时,即使我将正确的datetime值传递给控制器,我也会收到以下异常:
系统。当前使用的SQL Server版本不支持datetime2数据类型
其他stackoverflow主题提示日期时间未设置或编辑edmx文件,在本例中,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");
}
视图模型/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); }
}
}
}
模型/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; }
}
}
EF Core不支持SQL Server 2005,但支持2012及以后版本
https://learn.microsoft.com/en-us/ef/core/providers/sql-server/