Last post Dec 20, 2019 04:56 PM by bruce (sqlwork.com)
Dec 20, 2019 04:00 PMfirstname.lastname@example.org|LINK
i have an existing database, using this db created web application by .net core 3.1
now i wanna create models, tables are more than 100 so, cant create them manually so, i used "Scaffold-DbContext "Server=MyServer;Database=MyDB;Trusted_Connection=False;User ID=xxx;Password=myPwd;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models"
when i run this command received below exception. this is not only for one table
The column 'dbo.tappSearchResult01.selected' would normally be mapped to a non-nullable bool property, but it has a default constraint. Such a column is mapped to a nullable bool property to allow a difference between setting the property to
false and invoking the default constraint. See https://go.microsoft.com/fwlink/?linkid=851278 for details.
below is the table structure
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
CREATE TABLE [dbo].[tappSearchResult01](
[searchID] [int] NOT NULL,
[rowNumber] [int] NOT NULL,
[objectID] [int] NOT NULL,
[selected] [bit] NOT NULL,
CONSTRAINT [PK_tappSearchResult01] PRIMARY KEY CLUSTERED
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[tappSearchResult01] ADD CONSTRAINT [DF_tappSearchResult01_selected] DEFAULT ((1)) FOR [selected]
ALTER TABLE [dbo].[tappSearchResult01] WITH CHECK ADD CONSTRAINT [FK_tappSearchResult01_tappSearch] FOREIGN KEY([searchID])
REFERENCES [dbo].[tappSearch] ([searchID])
ALTER TABLE [dbo].[tappSearchResult01] CHECK CONSTRAINT [FK_tappSearchResult01_tappSearch]
may i know how to resolve this issue?
Dec 20, 2019 04:18 PM|bruce (sqlwork.com)|LINK
this is a limitation of using EF and POCO objects.
in the table design you made the column not null with a default. in sql the default is used if the insert does not include the column. but with EF, the not null means that the property can not be null, thus there is no way to make use of the default. in
c# value types like int, bool, etc can not null, unless they are of the nullable type.
you could drop the default constraints, scaffold and then add back (though they would never be used by EF).
Dec 20, 2019 04:37 PMemail@example.com|LINK
omg, dropping the constraints is sucide attempt. i wont.
so, may i know how to add .edmx file as the alternate way?
Dec 20, 2019 04:56 PM|bruce (sqlwork.com)|LINK
EF core has no support for .edmx files.
note: if you don't have the scripts that drop and recreate the default constraints, then just copy the database (schema only), and write a quick script to just drop default constraints on the copy. scaffold against the copied database, then delete it.