【问题标题】:Error ASP.NET Core EF Core and SQL Server 2005: The version of SQL Server in use does not support datatype 'datetime2'错误 ASP.NET Core EF Core 和 SQL Server 2005:正在使用的 SQL Server 版本不支持数据类型“datetime2”
【发布时间】: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


【解决方案1】:

EF Core 不支持 SQL Server 2005,但支持 2012 及更高版本

https://docs.microsoft.com/en-us/ef/core/providers/sql-server/

【讨论】:

  • 因为 ErikEJ 提供的链接现在重定向到主要的 EF 文档。您可以在此处找到直接链接 docs.microsoft.com/en-us/ef/core/providers/sql-server >“支持的数据库引擎 Microsoft SQL Server(2008 年以后)”
  • 截至今天,文档显示“支持的数据库引擎 Microsoft SQL Server(2012 起)”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2019-05-15
相关资源
最近更新 更多