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?

Joe Ruder
  • 2,122
  • 2
  • 23
  • 52

1 Answers1

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.

Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
  • This however is Database first, not code first so I should not change that from what I understand. – Joe Ruder Feb 12 '20 at 17:04
  • You can change the properties on the generated model, right? – Ricardo Peres Feb 12 '20 at 17:30
  • 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: // // 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. – Joe Ruder Feb 13 '20 at 09:34
  • The properties, we should never touch the generated code. – Ricardo Peres Feb 13 '20 at 09:42
  • 1
    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)." – Joe Ruder Feb 13 '20 at 09:58
  • I am sorry...I am not following where you are saying to set: "bool? NotificationSent { get; set; }" – Joe Ruder Feb 13 '20 at 09:59
  • Sorry, what I meant to say was, the property should be something like that. – Ricardo Peres Feb 13 '20 at 10:00
  • I have expanded the question with additional detail along with a image. I think we may be speaking of two different things. – Joe Ruder Feb 13 '20 at 11:27
  • No, that's the same thing: you don't want to set it to Computed, IMO. – Ricardo Peres Feb 13 '20 at 11:34
  • I have changed it from 'computed' to 'none' as stated in the question. But when I do that it ignores the default value. I can try manually setting the default value and I updated the question to reflect that and I will test. – Joe Ruder Feb 13 '20 at 11:53
  • I think you need to understand first my reasoning. Computed means that the value is assigned in the database, but that is different, I believe, from giving it a default value. Being assigned is for computed columns, columns that you cannot assign a value to directly. See https://stackoverflow.com/questions/5042327/entity-framework-storegeneratedpattern-computed-property. – Ricardo Peres Feb 13 '20 at 11:55
  • I totally understand that. I am 100% in agreement with you that I need that set to 'none' -- the question remains that when I do that then it does not use the default value as set in the database. After further research this seems to be an issue related to EF 6 and has been resolved in EF 7. For now it seems like I will need to manually update the 'default value' field in the properties. It does persist on a update, just not on a recreation. – Joe Ruder Feb 13 '20 at 12:00
  • You know, if it is an option, you might try to switch to EF Core. It works with .NET "classic", and it can generate classes from the database too. See this: https://learn.microsoft.com/en-us/ef/core/managing-schemas/scaffolding. – Ricardo Peres Feb 13 '20 at 12:01
  • 1
    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. – Joe Ruder Feb 13 '20 at 12:04