sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
706 stars 230 forks source link

Calculated Column Not Null #711

Open kcadduk opened 3 years ago

kcadduk commented 3 years ago

Creating a table

-- ****************** SqlDBM: Microsoft SQL Server ******************
-- ******************************************************************

-- ************************************** camo.LifeCode
CREATE TABLE camo.LifeCode
(
 ID                       int IDENTITY (1, 1) NOT NULL ,
 Type                     tinyint NOT NULL ,
 IsCalendar               AS CONVERT(BIT,CASE WHEN [Type] BETWEEN 0 AND 7 THEN 1 ELSE 0 END) PERSISTED NOT NULL ,
 IsUtilization            AS CONVERT(BIT,CASE WHEN [Type] BETWEEN 8 AND 10 THEN 1 ELSE 0 END) PERSISTED NOT NULL ,
 CONSTRAINT PK_tLifeCode PRIMARY KEY NONCLUSTERED (ID ASC),
);

Generates a model

public partial class LifeCode : RowVersionEntityBase
    {
        public int ID { get; set;  } // ID
        public byte Type { get; set; } // Type
        public bool? IsCalendar { get; private set; } // IsCalendar
        public bool? IsUtilization { get; private set; } // IsUtilization
    }

See how the calculated properties are nullable. This isn't the case. Is this a bug?

sjh37 commented 3 years ago

Using v3.4.1 with these settings:

Settings.DatabaseType = DatabaseType.SqlServer;
Settings.TemplateType = TemplateType.EfCore3;
Settings.GeneratorType = GeneratorType.EfCore;

and this sql

CREATE TABLE CalculatedColumnNotNull
(
    ID            INT     IDENTITY(1, 1) NOT NULL,
    [Type]        TINYINT NOT NULL,
    IsCalendar    AS CONVERT(BIT, CASE WHEN [Type] BETWEEN 0 AND  7 THEN 1 ELSE 0 END) PERSISTED NOT NULL,
    IsUtilization AS CONVERT(BIT, CASE WHEN [Type] BETWEEN 8 AND 10 THEN 1 ELSE 0 END) PERSISTED NOT NULL,
    CONSTRAINT PK_CalculatedColumnNotNull PRIMARY KEY NONCLUSTERED (ID ASC)
);
GO

I get the following code generated:

// CalculatedColumnNotNull
public class CalculatedColumnNotNull
{
    public int Id { get; set; } // ID (Primary key)
    public byte Type { get; set; } // Type
    public bool IsCalendar { get; private set; } // IsCalendar
    public bool IsUtilization { get; private set; } // IsUtilization
}

// CalculatedColumnNotNull
public class CalculatedColumnNotNullConfiguration : IEntityTypeConfiguration<CalculatedColumnNotNull>
{
    public void Configure(EntityTypeBuilder<CalculatedColumnNotNull> builder)
    {
        builder.ToTable("CalculatedColumnNotNull", "dbo");
        builder.HasKey(x => x.Id).HasName("PK_CalculatedColumnNotNull");

        builder.Property(x => x.Id).HasColumnName(@"ID").HasColumnType("int").IsRequired().ValueGeneratedOnAdd().UseIdentityColumn();
        builder.Property(x => x.Type).HasColumnName(@"Type").HasColumnType("tinyint").IsRequired();
        builder.Property(x => x.IsCalendar).HasColumnName(@"IsCalendar").HasColumnType("bit").IsRequired().ValueGeneratedOnAddOrUpdate();
        builder.Property(x => x.IsUtilization).HasColumnName(@"IsUtilization").HasColumnType("bit").IsRequired().ValueGeneratedOnAddOrUpdate();
    }
}

That looks ok to me. What version are you using?

kcadduk commented 3 years ago

3.3.0

No I'm not, 3.4.1

sjh37 commented 3 years ago

What settings do you have for

Settings.DatabaseType
Settings.TemplateType
Settings.GeneratorType
kcadduk commented 3 years ago

Sorry, same here.

image

sjh37 commented 3 years ago

I wonder if I've already fixed something, but not yet released as I have the not yet released v3.4.2 version. Copy the latest code from EF.Reverse.POCO.v3.ttinclude and paste it over your existing EF.Reverse.POCO.v3.ttinclude file, the save your `.tt' file to regenerate.

Please let me know if this does or does not work for you.