0

Hi all

I'm having some trouble with EF and SQL 2008 default values.

Firstly, I have a column called 'CreatedDate' and the default value in SQL Server is getdate(). If I add a field in SQL server this works fine. I also have a column called Guid and the default value of newid(). this also works fine.

My problem is that these do not see to be honoured in with the EF. I can get round this by setting these properties manually in the code but I'm not sure why this is or if I am supposed to do something different?

thanks

using(var ctx = new myEntities())
            {
                Company ct = new Company
                {
                    FullName = "Mynewco",
                    Code = "000110",
                    //Guid = Guid.NewGuid(), 
                    Language = "ENG", 
                    SystemCreatedUser = 1,
                    SystemLastEditDateTime = DateTime.UtcNow  
                };

                try
                {
                    ctx.AddToCompanies(ct);
                    ctx.SaveChanges();
                    Console.WriteLine("New Customer created with an ID of {0} and a Guid of {1}", ct.ID, ct.Guid);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException);
                }
                Console.Read(); 
            }
2
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by tig2810
0

The solution was to set 'StoredGeneratedPattern' to Computed against the property in the EF model.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.