0

I have the following Db table Person defined as

[PersonId] INT NOT NULL PRIMARY KEY IDENTITY,

[FirstName] VARCHAR(50),

[MiddleName] VARCHAR(50),

[LastName] VARCHAR(50),

[DOB] DATETIME,

[EmailAddress] VARCHAR(100),

[CreatedDt] DATETIME NOT NULL DEFAULT getDate(),

The corresponding Model for this table in the Web API project using EF 5.0 is

public int personId { get; set; }        

public string firstName { get; set; }

public string middleName { get; set; }

public string lastName { get; set; }

public Nullable<System.DateTime> dob { get; set; }

public string emailAddress { get; set; }

public System.DateTime createdDt { get; set; }

I am using EF Code First to Get and Set values in JSON format. The POST (Creating a new Person) generates an error {"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated."}

I then changed the Db column to [CreatedDt] **DATETIME2** NOT NULL DEFAULT getDate(), however now the CreatedDt is being saved as 01/01/0001 which is not what I want. I want the date to be set as the current database date.

When I try to pass the CreatedDt value as NULL from the Model it still does not work (as nulls are not allowed for this column). Can you please help me to set the value of this CreatedDt column to getDate() which is defined in the column definition in the database. I need to set the Current Database date so I cannot pass a current date from the Web API POST method as well.

4

1 回答 1

0

现在可以通过将这行代码放入 Model MAP 文件中

this.Property(t => t.createdDt).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

于 2013-09-21T06:03:23.917 回答