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(); 
            }

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.