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

标签 entity-framework sql-server-2005 entity-framework-6 asp.net-core-mvc

即使我将正确的日期时间值传递给 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/

相关文章:

c# - DbExecutionStrategy 重试次数超过 MaxRetryCount

ef-code-first - 使用 Entity Framework 6 删除表

c# - 包含路径表达式必须引用 type.in 预加载中定义的导航属性

c# - Get Entity Framework 6 在其下方的 SELECT 语句中使用 NOLOCK

c# - LINQ 按子属性分组

c# - 无法使用 3 层架构添加/更新

sql-server - SQL Server : conventions for naming a schema

c# - 如何使用 C# 将数据属性添加到下拉菜单

sql-server-2005 - 在 SSRS 2005 IIF 表达式中使用 Like 运算符

oracle - 从 Oracle 数据建模器创建到 SQL Server 数据库的连接