1

I am very new to entity framework. Nice concept and fast as well. Working in c#.net right now. I have stucked here where datetime comes in picture.

I mean....

lets assume i have user table in DB where CreatedDate fields is there in with datetime datatype in sql db.

My entity framework works like this... when i need to add object to db, i simply pass objUser.createdDate = DateTime.now.

How ever I want to change the concept for some requirement changes.

I need to store sql server DateTime() for createdDate field in table.

How can i do that???

any idea...please help. follwoing is a just sample code of my project.

      objCustomer.RegisterDate = DateTime.Now;
            objCustRepository.AddCustomer(objCustomer);

I want to remove this DateTime.Now line and maintain through sql....

2 Answers 2

3

if you can store the value from stored procedure instead of your business logic then you can try it by following.

to store SQL Datetime value then you can also use GETDATE() function in SQL.

or

you can define RegisterDate default value = GETDATE()

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

1 Comment

You are right, actaully we can default value works. what if i have set that and supply the date from code behind? will take the default value??
0

It sounds like you are trying to target the specific timezone associated with your sql server, if I am not mistaken?

If so why not use DateTimeOffset.Now via your c# when setting the date.

See this blog post here about the type.

A DateTimeOffset represents a specific point in time, so you can use it to convert to any timezone representation you may need on your UI.

Be aware though that it is only supported on SQL Server 2008 and greater, if I remember correctly. :)

Comments

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.