0

I am using EF 5 and SQL Server 2005, Model First (sort of).

By sort of, I mean that I typically build my schema in the SQL Server designer, but import the schema into EF so I have a visual view. There is often round-tripping.

However, I noticed that when I try to generate the DB schema based on the EF model, it skips all of the NEWID() default values that I have assigned as default values to my Guid IDs, but it doesn't skip the identity fields of type int.

I found this post explaining the reasoning for this: Entity Framework 4 and Default Values

However, it doesn't answer my question: How do I get Entity Framework to generate a SQL DDL database schema with default values of NEWID() for my uniqueidentifier types?

NOTE:

I don't care about how to set them from the POCO entities and so forth (there are plenty of posts describing that) - my concern is getting the SQL DDL generated right so I can seed the database without worrying about these values going missing.

1 Answer 1

2

Using Entity Framework Migrations, you can use the GUID column builder and its DefaultValueSql parameter. The value of that parameter can be the string "NEWID()". This should take care of proper DDL generation.

Next you should declare these properties as database-generated using attributes or the fluent model builder, so that EF ignores the values set in your POCOs (which will be null for new objects).

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

2 Comments

Thanks. Entity framework migrations is for code first, so I am not sure if it will help anyone who comes across this question. I ended up switching to code first several months ago anyway and more recently to code first migrations. +1 for your effort, but will only accept an answer that is for model first as the correct one.
You're right, we switched so long ago I didn't even realize. Oh well ;).

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.