Liam Fell
Liam Fell

Reputation: 1320

Entity Framework Seed Method DateTime Error

I have a strange error seeding my SQL database. The class code is shown below. I have a syntax error when entering the DateTime value for

'EventTime' however; "Syntax error, ',' expected".

This is the correct DateTime value syntax as far as I'm aware, if I enter the same value in to the table directly it works. Does anyone know how I can rectify this, thanks.

namespace WLL.Models

{
    public class ProductDatabaseInitializer : DropCreateDatabaseIfModelChanges<ProductContext>
    {
        protected override void Seed(ProductContext context)
        {
            GetEvents().ForEach(e => context.Events.Add(e));

        }

        private static List<Event> GetEvents()
        {
            var events = new List<Event> {
                new Event
                {
                    EventID = 1,
                    EventTime = 15/06/2015 13:45:00,
                    ProductID = 1,

                },

            };

            return events;
        }

Upvotes: 0

Views: 2725

Answers (3)

Kishore Kumar
Kishore Kumar

Reputation: 12874

You can use

var events = new List<Event> {
            new Event
            {
                EventID = 1,
                EventTime = DateTime.ParseExact("15/06/2015 13:45:00", "dd/MM/yyyy HH:mm:ss",null),
                ProductID = 1,

            },

        };

Upvotes: 2

CodeCaster
CodeCaster

Reputation: 151604

You're writing C# there, not SQL.

Use the DateTime constructor EventTime = new DateTime(2015, 06, 15, 13, 45, 00).

Upvotes: 1

Andre Pena
Andre Pena

Reputation: 59336

Try using one of these DateTime constructors.. The literal you are entering doesn't exist in C#.

Try replacing your DateTime with this:

    private static List<Event> GetEvents()
    {
        var events = new List<Event> {
            new Event
            {
                EventID = 1,
                EventTime =  new DateTime(2015, 5, 15, 13, 45, 0),
                ProductID = 1,
            },

        };

        return events;
    }

Upvotes: 1

Related Questions