2

I receive the following exception when attempting to update a database record even though I was passing in the correct datetime values to the controller.

System.ArgumentException: The version of SQL Server in use does not support datatype 'datetime2'

Other stackoverflow topics suggest either the datetime has not been set or to edit the edmx file, which in this case, doesn't exist. I have tested the application with a localdb context in Visual Studio 2015 but I only receive an error when trying to connect to the SQL Server 2005 DbContext.

Is this exception due to an incompatibility with SQL Server 2005 or is there a workaround which I'm missing?

Controllers/SecureController/Save

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; }
    }
}
5
  • The datatype datetime2is not in SQL Server 2005. It's 2008 and up Commented Sep 15, 2016 at 7:51
  • I haven't specified datetime2 in my SubscriptionsViewModel class; only datetime. Commented Sep 15, 2016 at 8:00
  • is this code first? Commented Sep 15, 2016 at 8:06
  • Yes, albeit, without migrations. Commented Sep 15, 2016 at 8:09
  • can you put SubscriptionsViewModel code also? Commented Sep 15, 2016 at 8:12

1 Answer 1

7

EF Core does not support SQL Server 2005, but 2012 and forward

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

Sign up to request clarification or add additional context in comments.

2 Comments

as the link ErikEJ provided now redirects to the main EF documentation. You can find the direct link here learn.microsoft.com/en-us/ef/core/providers/sql-server > "Supported Database Engines Microsoft SQL Server (2008 onwards)"
As of today, the documentation says "Supported Database Engines Microsoft SQL Server (2012 onwards)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.