locked
Scaffold-DbContext Such a column is mapped to a nullable bool property RRS feed

  • Question

  • User-1471881183 posted

    hello all,

    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
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    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 
    (
    	[searchID] ASC,
    	[rowNumber] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[tappSearchResult01] ADD  CONSTRAINT [DF_tappSearchResult01_selected]  DEFAULT ((1)) FOR [selected]
    GO
    
    ALTER TABLE [dbo].[tappSearchResult01]  WITH CHECK ADD  CONSTRAINT [FK_tappSearchResult01_tappSearch] FOREIGN KEY([searchID])
    REFERENCES [dbo].[tappSearch] ([searchID])
    GO
    
    ALTER TABLE [dbo].[tappSearchResult01] CHECK CONSTRAINT [FK_tappSearchResult01_tappSearch]
    GO
    
    
    

    may i know how to resolve this issue?

    Friday, December 20, 2019 4:00 PM

All replies

  • User-474980206 posted

    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).

     

    Friday, December 20, 2019 4:18 PM
  • User-1471881183 posted

    omg, dropping the constraints is sucide attempt. i wont.

    so, may i know how to add .edmx file as the alternate way?

    Friday, December 20, 2019 4:37 PM
  • User-474980206 posted

    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.

    Friday, December 20, 2019 4:56 PM