3

I thought I saw something somewhere about being able to generate an object in EF with all the non-null values filled in with generic values.

I tried using context.CreateObject<MyEntity>(), however I am still getting errors about trying to save NULL data to a non-null column.

Was I mistaken in thinking this? Or do I just have the wrong syntax?

2 Answers 2

3

In the model designer, you can enter a default value on the property sheet for each field.

enter image description here

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

3 Comments

I was hoping there was a generic way to avoid that. The database table is not mine and has around 50 non-null columns, of which I am really only concerned with updating a handful.
@Rachel, sorry, I meant the Entity Model designer in VS.
That was what I was going to do if there wasn't some easy syntax to do that for me. I thought I read somewhere that there was though...
2

You can not save null data to a non-nullable column. If CreateObject method gives you that you'll need to change that before trying to save data. Also, watch out for DateTime objects, SQL server has different min value of that than the .NET.

3 Comments

Yeah, I've had issues with the DateTime values in the past because of that. Who thought it would be a good idea to make a minimum date of 1/1/1753??
@Rachel its the year after the change to the Gregorian calendar. 1752 had some days missing to make the adjustment and they didn't want to add complexity to handle it. If you need the min sql server date in .NET you can get it with SqlDateTime.MinValue.
@PeteT Thanks, I didn't know that. I've always hardcoded it :)

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.