Snow
Snow

Reputation: 87

DateTime property in EntityFramework Core incorrectly adds data to SQL Server

I have a DateTime property on the model:

public class OrderTimeModel
    {
    DateTime orderTime;

    public DateTime OrderTime { get { return orderTime; } set { orderTime = DateTime.Now; } }
    }

I have the wrong date and time in SQL Server: 01.01.0001 0:00:00

Upvotes: 0

Views: 38

Answers (2)

Snow
Snow

Reputation: 87

Ok, I found where I made a mistake.

public class OrderTimeModel
{
public DateTime orderTime = DateTime.Now;
public DateTime OrderTime { get { return orderTime; } set { orderTime = value; }
}

Upvotes: 1

Thomas Koelle
Thomas Koelle

Reputation: 3742

I think you want something like:

public DateTime OrderTime { get; set } = DateTime.Now;

Because set is not the default value, it is what you set it with.

Upvotes: 1

Related Questions