gpt4 book ai didi

entity-framework - 错误 ASP.NET Core EF Core 和 SQL Server 2005 : The version of SQL Server in use does not support datatype 'datetime2'

转载 作者:行者123 更新时间:2023-12-01 22:21:51 25 4
gpt4 key购买 nike

即使我将正确的日期时间值传递给 Controller ​​,我在尝试更新数据库记录时收到以下异常。

System.ArgumentException:正在使用的 SQL Server 版本不支持数据类型“datetime2”

其他 stackoverflow 主题建议未设置日期时间或编辑 edmx 文件,在本例中,该文件不存在。我已经在 Visual Studio 2015 中使用 localdb 上下文测试了该应用程序,但我仅在尝试连接到 SQL Server 2005 DbContext 时收到错误。

此异常是由于与 SQL Server 2005 不兼容造成的,还是我缺少解决方法?

Controller /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); }
}
}
}

模型/订阅.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/

关于entity-framework - 错误 ASP.NET Core EF Core 和 SQL Server 2005 : The version of SQL Server in use does not support datatype 'datetime2' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39505664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com