8

I have a LoginRecord table in sqlserver 2008 with the following column structure-

LoginId      - int, identity
UserId       - int
LoginDateTime- Allow nulls false,default value getdate()

I am inserting new record by entity framework 6 as below-

 db.LoginRecords.Add(new LoginRecord() { UserId = UserId }); 
 db.SaveChanges();

But in LoginDateTime table, null value is being inserted. It supposed to be current datetime.

I am using database first approach.

How can overcome this issue?

8
  • Try new LoginRecord() { UserId = UserId, LoginDateTime = null }); Commented Mar 16, 2014 at 11:32
  • @juergen d, its not solving that problem. Commented Mar 16, 2014 at 11:40
  • Mark your date property with DatabaseGenerated attribute Commented Mar 16, 2014 at 11:43
  • Try setting the "StoredGeneratedProperty" attribute of your datetime in the EDMX file to Computed. From the following thread: stackoverflow.com/a/4688135/2488939 Commented Mar 16, 2014 at 11:45
  • 2
    Go to the edmx file designer by clicking on your edmx file. Then locate your table and the property. Right-click the column in the table that you want to change and click on properties. The property window should then come up and you will see as one of the properties "StoredGeneratedProperty". Change that to computed. Commented Mar 16, 2014 at 11:54

3 Answers 3

7

Combined my two comments into an answer.

Try setting the "StoredGeneratedPattern" attribute of your datetime in the EDMX file to Computed. From the following thread: http://www.stackoverflow.com/a/4688135/2488939

To do this, go to the edmx file designer by clicking on your edmx file. Then locate your table and the property. Right-click the column in the table that you want to change and click on properties. The property window should then come up and you will see as one of the properties "StoredGeneratedPattern". Change that to computed.

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

Comments

2

In addition to changing the EDMX file as suggested by Vishwaram Maharaj, you should make the definition of the table match between EF and the DB. The table description of "LoginDateTime- Allow nulls false" is itself false. The field clearly allows NULLs if NULLs are being inserted. Alter the column to not allow NULL if it truly shouldn't have NULL values in it:

ALTER TABLE LoginRecords ALTER COLUMN LoginDateTime DATETIME NOT NULL;

Comments

-1

Setting default values in Entity Framework 5 and 6 by changing T4 Template File Made below changes in .tt(template file) Example: red means remove and green means add enter image description here This will add constructor in all entity classes with OnCreated method.

Like below

public partial class Category
{
    public Category()
    {
        this.Products = new HashSet<Product>();
        OnCreated();
    }

    partial void OnCreated();
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

Then create class file using same namespace that of Entities.

public partial class Category
{
    partial void OnCreated()
    {
        Name = "abc"
    }
}

Refer this https://www.youtube.com/watch?v=i8J2ipImMuU

Helpfull

1 Comment

please summarize the video

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.