2

Using SQL and EF 6 (C#), I have read multiple posts on this but with no clear solution.

The problem is simple, I want to have a 'default unless otherwise provided' value in the database.

So, I set the default in the database like so:

[notification_sent] BIT NULL DEFAULT ((0)),

Then turn the StoreGeneratedPattern to 'Computed' in the EDMX (using database first).

Is there not a way to now set this field to true?

Below is what the Properties looks like from in VS. If I put this property back to 'none' then it overwrites the value with NULL if I do not provide a value in my code vs using the Default value of 0 as set in the database.
It seems to me that it should be filling in the 'Default Value' field here when it (Visual Studio) generates the .edmx from the database.

properties window

I have tried setting 'Nullable' in the above properties to 'true' and it still shows the exact same as before:

public Nullable<bool> notification_sent { get; set; }

In the generated .cs

If I manually insert a value of 'false' in the 'Default Value' field it add the below:

this.notification_sent = false;

But will this not be overwritten when ever I do a refresh from the database?

2
  • thanks for the edit Marc_s -- I should not have rushed writing it so much. Commented Feb 12, 2020 at 17:07
  • The question is quite old, but ever got a satisfying solution to this? Commented Nov 18, 2020 at 12:47

1 Answer 1

0

You don't want to set the StoreGeneratedPattern to Computed, because this means that it's the database's responsibility to create the value for you. You are right in creating a value that is nullable with a default value, but in that case you should map it to a nullable boolean, e.g., your property should look something like this:

bool? NotificationSent { get; set; }

Or some other name. You can either set a value or not, if you don't, the database will set it for you, as 0 (false). This has nothing to do with being computed or not, it's just the default value.

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

14 Comments

This however is Database first, not code first so I should not change that from what I understand.
You can change the properties on the generated model, right?
Yes, I can change them. Are you referring to the 'properties' tab from the model.edmx screen? Or are you talking about the actual model itself (table_name.co) which has: // <auto-generated> // This code was generated from a template. // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated.
So, yes...that is how I set the property referred to in the question: "Then turn the StoreGeneratedPattern to 'Computed' in the EDMX (using database first)."
thank you....I am using Core in all new development and you are correct, it is much better. This is a legacy app and would prefer to not have to switch right now. I think you have pointed me the right direction enough on this, I just need to test tomorrow and should be able to close this out.
|

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.