-1

I have created my domain using the below command and it works fine

Scaffold-DbContext -Connection name=MyConnectionStringName -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force -Project My.Domain -StartupProject My.Api

I am migrating some code from a manual process of creating the domains, the original didn't have the AuditApp column defined which is defaulted in the database to the App=MyApi in the connection string. Here is the default constraint defined in the database:

ALTER TABLE [dbo].[PriceGroups] ADD  CONSTRAINT [DF_PriceGroups_AuditApp]  DEFAULT (rtrim(isnull(app_name(),''))) FOR [AuditApp]

Now that I have this column in the scaffold output I get this error when I try and add an object

Cannot insert the value NULL into column 'AuditApp'

Is there a way for me to bypass this so that it doesn't pass it? or am I going to have to do a manual domain approach :/ ?

...
public string AuditApp { get; set; }
...

I will be wanting to run the scaffold tool often so need a solution that does not mean I have to change the auto generated code.

Andrew
  • 2,571
  • 2
  • 31
  • 56
  • If I understand you correctly, you want to [treat it as a read-only property](https://stackoverflow.com/questions/6564772/ef-code-first-readonly-column). – Ludo Aug 19 '21 at 13:29

1 Answers1

1

Please, to meet your requirements quickly and guarantees to not break your application with this error, at property AuditApp, try add a default value, like this:

...
public string AuditApp { get; set; } = string.Empty;
...
Antonio Leonardo
  • 1,805
  • 1
  • 8
  • 18
  • So it looks like it is not possible if I want to keep generating a scaffold after each change. If my database didn't have default values then i would be ok. So in short once ive created the scaffold I have to maintain it manually and just add defaults like Antonio has said. not really what I wanted to hear but cheers. – Andrew Aug 19 '21 at 14:02