URGENT: critical bug, generated code differs from diagram models

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Tue 23 Mar 2021 14:49

Standalone version 6.10.1189

The generated code is inconsistance.
Class model is correct
ApplicationDbContext is incorrect (does not match diagram, mapping is incorrect)
SQL code is incorrect (does not match diagram)
Entity Developer files: https://filebin.net/eejz1tnik5a74u5e

Below is the generated code where all prosperities (except for primary key) is set to nullable
https://pasteboard.co/JTXkJl8.png

The root cause of this issue (the efml information are saved incorrectly)
p1:nullable="True" <-- this is right
not-null="True" <-- this is wrong it should be saved as "not-null="False" because the column is set to nullable.

Code: Select all

    <property name="MetaData" type="String" p1:nullable="True" p1:ValidateRequired="false" p1:Guid="6a3e0473-d959-4044-ac85-dce9ad4a18d4">
      <column name="meta_data" not-null="True" sql-type="varchar" p1:unicode="True" />
This will generated the incorrect mapping

Code: Select all

            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();


Generated code - incorrect code generated (does not match model diagram)
Test.ApplicationDbContext.cs

Code: Select all

//------------------------------------------------------------------------------
// This is auto-generated code.
//------------------------------------------------------------------------------
// This code was generated by Entity Developer tool using EF Core template.
// Code is generated on: 24/03/2021 1:32:48 AM
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Test.DataModels;

namespace Test.Infrastructure.Data
{

    public partial class ApplicationDbContext : DbContext
    {

        public ApplicationDbContext() :
            base()
        {
            OnCreated();
        }

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
            base(options)
        {
            OnCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured ||
                (!optionsBuilder.Options.Extensions.OfType<RelationalOptionsExtension>().Any(ext => !string.IsNullOrEmpty(ext.ConnectionString) || ext.Connection != null) &&
                 !optionsBuilder.Options.Extensions.Any(ext => !(ext is RelationalOptionsExtension) && !(ext is CoreOptionsExtension))))
            {
            }
            CustomizeConfiguration(ref optionsBuilder);
            base.OnConfiguring(optionsBuilder);
        }

        partial void CustomizeConfiguration(ref DbContextOptionsBuilder optionsBuilder);

        public virtual DbSet<Card> Cards
        {
            get;
            set;
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            this.CardMapping(modelBuilder);
            this.CustomizeCardMapping(modelBuilder);

            RelationshipsMapping(modelBuilder);
            CustomizeMapping(ref modelBuilder);
        }

        #region Card Mapping

        private void CardMapping(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Card>().ToTable(@"card", @"public");
            modelBuilder.Entity<Card>().Property(x => x.Id).HasColumnName(@"id").HasColumnType(@"uuid").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.Nickname).HasColumnName(@"nickname").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.FirstName).HasColumnName(@"first_name").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.LastName).HasColumnName(@"last_name").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.Name).HasColumnName(@"`name`").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.Number).HasColumnName(@"`number`").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.ExpirationMonth).HasColumnName(@"expiration_month").HasColumnType(@"int4").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.ExpirationYear).HasColumnName(@"expiration_year").HasColumnType(@"int4").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingAddressLine1).HasColumnName(@"billing_address_line1").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingAddressLine2).HasColumnName(@"billing_address_line2").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingCountryState).HasColumnName(@"billing_country_state").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingTownCity).HasColumnName(@"billing_town_city").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingPostalCode).HasColumnName(@"billing_postal_code").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.BillingCountry).HasColumnName(@"billing_country").HasColumnType(@"varchar").ValueGeneratedNever();
            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
            modelBuilder.Entity<Card>().HasKey(@"Id");
        }

        partial void CustomizeCardMapping(ModelBuilder modelBuilder);

        #endregion

        private void RelationshipsMapping(ModelBuilder modelBuilder)
        {
        }

        partial void CustomizeMapping(ref ModelBuilder modelBuilder);

        public bool HasChanges()
        {
            return ChangeTracker.Entries().Any(e => e.State == Microsoft.EntityFrameworkCore.EntityState.Added || e.State == Microsoft.EntityFrameworkCore.EntityState.Modified || e.State == Microsoft.EntityFrameworkCore.EntityState.Deleted);
        }

        partial void OnCreated();
    }
}

Test.Card.cs - correct code is generated (matches model diagram)

Code: Select all


//------------------------------------------------------------------------------
// This is auto-generated code.
//------------------------------------------------------------------------------
// This code was generated by Entity Developer tool using EF Core template.
// Code is generated on: 24/03/2021 1:32:48 AM
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;

namespace Test.DataModels
{
    public partial class Card {

        public Card()
        {
            OnCreated();
        }

        [Key]
        [Required()]
        public virtual Guid Id { get; set; }

        public virtual string Nickname { get; set; }

        public virtual string FirstName { get; set; }

        public virtual string LastName { get; set; }

        public virtual string Name { get; set; }

        public virtual string Number { get; set; }

        public virtual int? ExpirationMonth { get; set; }

        public virtual int? ExpirationYear { get; set; }

        public virtual string BillingAddressLine1 { get; set; }

        public virtual string BillingAddressLine2 { get; set; }

        public virtual string BillingCountryState { get; set; }

        public virtual string BillingTownCity { get; set; }

        public virtual string BillingPostalCode { get; set; }

        public virtual string BillingCountry { get; set; }

        public virtual string MetaData { get; set; }

        #region Extensibility Method Definitions

        partial void OnCreated();

        #endregion
    }

}

Sql code - incorrect sql code, the only statement with " NOT NULL" should be the primary key only.

Code: Select all

-- Script was generated by Devart Entity Developer, Version 6.10.1189.0
-- Script date 24/03/2021 2:01:56 AM
-- Target Server: PostgreSQL
-- Server Version: 13.0

-- 
-- Creating a table public.card 
-- 
CREATE TABLE public.card (
   id UUID NOT NULL,
   nickname VARCHAR,
   first_name VARCHAR,
   last_name VARCHAR,
   "name" VARCHAR NOT NULL,
   "number" VARCHAR NOT NULL,
   expiration_month INT4 NOT NULL,
   expiration_year INT4 NOT NULL,
   billing_address_line1 VARCHAR NOT NULL,
   billing_address_line2 VARCHAR,
   billing_country_state VARCHAR,
   billing_town_city VARCHAR NOT NULL,
   billing_postal_code VARCHAR NOT NULL,
   billing_country VARCHAR,
   meta_data VARCHAR NOT NULL,
   CONSTRAINT "PK_card" PRIMARY KEY (id)
);

Last edited by RobertK on Tue 23 Mar 2021 15:15, edited 4 times in total.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: Generated code differs from design models - 2

Post by RobertK » Tue 23 Mar 2021 15:11

Note: this issue does not occur with only one class model, this issue affects hundred of tables, diagram does not match the code/table generated.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by Shalex » Thu 25 Mar 2021 17:48

RobertK wrote: Tue 23 Mar 2021 14:49 The root cause of this issue (the efml information are saved incorrectly)
p1:nullable="True" <-- this is right
not-null="True" <-- this is wrong it should be saved as "not-null="False" because the column is set to nullable.

Code: Select all

    <property name="MetaData" type="String" p1:nullable="True" p1:ValidateRequired="false" p1:Guid="6a3e0473-d959-4044-ac85-dce9ad4a18d4">
      <column name="meta_data" not-null="True" sql-type="varchar" p1:unicode="True" />
This will generated the incorrect mapping

Code: Select all

            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
To fix the issue, please clear selection of the Not Null attribute in Column Editor: https://prnt.sc/10vafdl.

After this, the generated code would be:

Code: Select all

            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").ValueGeneratedNever();
Also, you can run Regenerate Storage and Mapping to clear data in Column Editor for all properties: https://prnt.sc/10vaibd.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Thu 15 Apr 2021 16:31

Shalex wrote: Thu 25 Mar 2021 17:48
RobertK wrote: Tue 23 Mar 2021 14:49 The root cause of this issue (the efml information are saved incorrectly)
p1:nullable="True" <-- this is right
not-null="True" <-- this is wrong it should be saved as "not-null="False" because the column is set to nullable.

Code: Select all

    <property name="MetaData" type="String" p1:nullable="True" p1:ValidateRequired="false" p1:Guid="6a3e0473-d959-4044-ac85-dce9ad4a18d4">
      <column name="meta_data" not-null="True" sql-type="varchar" p1:unicode="True" />
This will generated the incorrect mapping

Code: Select all

            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").IsRequired().ValueGeneratedNever();
To fix the issue, please clear selection of the Not Null attribute in Column Editor: https://prnt.sc/10vafdl.

After this, the generated code would be:

Code: Select all

            modelBuilder.Entity<Card>().Property(x => x.MetaData).HasColumnName(@"meta_data").HasColumnType(@"varchar").ValueGeneratedNever();
Also, you can run Regenerate Storage and Mapping to clear data in Column Editor for all properties: https://prnt.sc/10vaibd.

thanks this fixed the problem.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Sat 22 May 2021 17:58

Regenerate model and remapping still has issues as reported here viewtopic.php?f=32&t=45661

Alternative solution is to clear selection of the Not Null attribute in Column Editor: https://prnt.sc/10vafdl. <-- I can not do this because I don't know which column follows or does not the model diagram, it's very difficult determine if the generated code is correct aswell because it is not always consistent with the model diagram and I can't fix this either,its a very big problem.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Sun 23 May 2021 06:13

Shalex wrote: Thu 25 Mar 2021 17:48 ...
Is it possible to always make entity developer generated code match the class model diagram? (instead entity developer generating code that sometimes match the diagram and sometimes it does not match, its often inconsistent)

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by Shalex » Tue 25 May 2021 13:04

RobertK wrote: Sun 23 May 2021 06:13 Is it possible to always make entity developer generated code match the class model diagram? (instead entity developer generating code that sometimes match the diagram and sometimes it does not match, its often inconsistent)
Settings of a class property should always be consistent with the ones of the corresponding column using both Model-First and Database-First approaches if you leave default settings created by Entity Developer. Please give us a step-by-step walkthrough we should follow to reproduce inconsistency in our environment.

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Wed 26 May 2021 18:17

Shalex wrote: Tue 25 May 2021 13:04
RobertK wrote: Sun 23 May 2021 06:13 Is it possible to always make entity developer generated code match the class model diagram? (instead entity developer generating code that sometimes match the diagram and sometimes it does not match, its often inconsistent)
Settings of a class property should always be consistent with the ones of the corresponding column using both Model-First and Database-First approaches if you leave default settings created by Entity Developer. Please give us a step-by-step walkthrough we should follow to reproduce inconsistency in our environment.
It's a bug, so it's often inconsistent,... It was reported here viewtopic.php?f=32&t=45661&sid=c08446ac ... e1c6142c7b

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by Shalex » Fri 28 May 2021 04:56

RobertK wrote: Wed 26 May 2021 18:17 It's a bug, so it's often inconsistent,... It was reported here viewtopic.php?f=32&t=45661&sid=c08446ac ... e1c6142c7b
Regenerate Storage and Mapping Wizard in EF Core Model will help to fix inconsistency of the existing model.

Could you tell us the steps we should follow with a brand-new EF Core Model to reproduce inconsistency caused by Entity Developer?

RobertK
Posts: 111
Joined: Thu 02 Mar 2017 05:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by RobertK » Thu 03 Jun 2021 17:28

Shalex wrote: Fri 28 May 2021 04:56
RobertK wrote: Wed 26 May 2021 18:17 It's a bug, so it's often inconsistent,... It was reported here viewtopic.php?f=32&t=45661&sid=c08446ac ... e1c6142c7b
Regenerate Storage and Mapping Wizard in EF Core Model will help to fix inconsistency of the existing model.

Could you tell us the steps we should follow with a brand-new EF Core Model to reproduce inconsistency caused by Entity Developer?
Was the bug fixed? Regenerate storage and mapping should fix it but it didn't, if there's an updated version, will try that.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by Shalex » Fri 11 Jun 2021 18:49

The behavior of the Regenerate Storage and Mapping functionality in EF Core, NHibernate, and Telerik Data Access models is changed: the NOT NULL attribute of the column is reset to undefined now; after that, the behavior depends on the Nullable attribute of the property only.

There is no license associated with your email used when registering on the forum. Please contact us and specify your licensed email and we will send you the internal build with the fix.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: URGENT: critical bug, generated code differs from diagram models

Post by Shalex » Fri 09 Jul 2021 09:19

New build of Entity Developer 6.11.1283 is available for download now: viewtopic.php?f=32&t=47208.

Post Reply