wrong length for nvarchar(max)

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

wrong length for nvarchar(max)

Post by kindbergs.dk » Tue 06 Dec 2016 10:12

Hi,
Working database first, when updating the model in Entity Developer for nHibernate version 6.0.116 based on an SQL Server 2008 database, nvarchar(max) database fields get a default max column length and validation length of 4000, which breaks flushing.
Can you update the program to automatically set the allowed string length for nvarchar(max) similar to ntext fields?
(Or if you think it already is, can the bug be caused by some legacy SQL Server compatibility mode? My database and model started as SQL Server 2000, and we are in the progress to update it via SQL Server 2008 to the latest version, which includes changing legacy ntext fields to nvarchar(max), but the problem with 4000 as max comes both for old changed fields and newly added fields.)
Best
Allan K.

kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

Re: wrong length for nvarchar(max)

Post by kindbergs.dk » Tue 06 Dec 2016 10:16

Plus fix similar situation for varbinary(max)

Pinturiccio
Devart Team
Posts: 2192
Joined: Wed 02 Nov 2011 09:44

Re: wrong length for nvarchar(max)

Post by Pinturiccio » Wed 07 Dec 2016 15:21

We have fixed the bug with setting incorrect length for varchar(max), nvarchar(max), and varbinary(max) SQL Server columns added by Create Model Wizard and Update Model From Database Wizard (Entity Framework and NHibernate models). We will post here when the corresponding build of Entity Developer is available for download.

kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

Re: wrong length for nvarchar(max)

Post by kindbergs.dk » Sat 10 Dec 2016 12:41

Hi, saw there was a new version with a fix and tested it.
When updating the model from the database, it suggests varbinary(max) should get a new length of -1, but it is the only change in those tables, and it does not get stored/changed in the model despite selecting it. (Had similar experience when the only change is in the comments of a database field - despite selecting it, the change is never stored unless other new fields are added too.)
For nvarchar(max), I found that it only suggest changing the length to -1 if it had the old bad default of 4000, which might be understandable, but similar to varbinary the change is not persisted in the model.
Hope you can also fix this other bug regarding "minor changes" not being stored :-)

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

Re: wrong length for nvarchar(max)

Post by Shalex » Thu 15 Dec 2016 13:09

Sorry, but the problem is not clear. We have created a table in the database:

Code: Select all

CREATE TABLE [dbo].[tmax](
 [id] [int] NOT NULL PRIMARY KEY,
 [varcharmax] [varchar](max) NULL,
 [nvarcharmax] [nvarchar](max) NULL,
 [varbinarymax] [varbinary](max) NULL
);
Added the table to the *.hbml model with enabled NHibernate and Fluent NHibernate templates. The generated mapping with Entity Developer v6.0.147 is the following:

XML mapping

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="myModel" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Tmax" table="tmax" schema="dbo">
    <id name="Id" type="Int32">
      <column name="id" not-null="true" precision="10" scale="0" sql-type="INT" />
      <generator class="assigned" />
    </id>
    <property name="Varcharmax" type="String">
      <column name="varcharmax" not-null="false" sql-type="VARCHAR(MAX)" />
    </property>
    <property name="Nvarcharmax" type="String">
      <column name="nvarcharmax" not-null="false" sql-type="NVARCHAR(MAX)" />
    </property>
    <property name="Varbinarymax" type="Byte[]">
      <column name="varbinarymax" not-null="false" sql-type="VARBINARY(MAX)" />
    </property>
  </class>
</hibernate-mapping>
Fluent mapping

Code: Select all

using System;
using System.Collections;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using FluentNHibernate.Mapping;
using FluentNHibernate.MappingModel.Collections;

namespace myModel
{
    /// <summary>
    /// There are no comments for TmaxMap in the schema.
    /// </summary>
    public partial class TmaxMap : ClassMap<Tmax>
    {
        /// <summary>
        /// There are no comments for TmaxMap constructor in the schema.
        /// </summary>
        public TmaxMap()
        {
              Schema(@"dbo");
              Table(@"tmax");
              LazyLoad();
              Id(x => x.Id)
                .Column("id")
                .CustomType("Int32")
                .Access.Property()
                .CustomSqlType("int")
                .Not.Nullable()
                .Precision(10)
                .GeneratedBy.Assigned();
              Map(x => x.Varcharmax)    
                .Column("varcharmax")
                .CustomType("String")
                .Access.Property()
                .Generated.Never()
                .CustomSqlType("varchar");
              Map(x => x.Nvarcharmax)    
                .Column("nvarcharmax")
                .CustomType("String")
                .Access.Property()
                .Generated.Never()
                .CustomSqlType("nvarchar");
              Map(x => x.Varbinarymax)    
                .Column("varbinarymax")
                .CustomType("Binary")
                .Access.Property()
                .Generated.Never()
                .CustomSqlType("varbinary");
              ExtendMapping();
        }

        #region Partial Methods

        partial void ExtendMapping();

        #endregion
    }

}
Please point to the issue we should fix.

kindbergs.dk
Posts: 13
Joined: Sun 27 Jan 2013 21:14

Re: wrong length for nvarchar(max)

Post by kindbergs.dk » Thu 15 Dec 2016 20:30

Hi,
I don't normally look at the XML and I don't see any length at in yours, but if you in Entity Developer set the length of any of the (max) fields to 4000 and the try to update the model from the database, Entity Developer will suggest changing the length from 4000 to -1, but it never happens - each time you run the update model from database, you get the same suggestion.
Similarly if you add a database comment to a field, Entity Developer will suggest copying the changed comment to the model, but it does not happen, and will appear as a new change every time you syncronize.
Somehow, certain database table/field changes are "too small" to trigger updating the XML/model.

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

Re: wrong length for nvarchar(max)

Post by Shalex » Tue 20 Dec 2016 14:31

kindbergs.dk wrote:if you in Entity Developer set the length of any of the (max) fields to 4000 and the try to update the model from the database, Entity Developer will suggest changing the length from 4000 to -1, but it never happens - each time you run the update model from database, you get the same suggestion.
Similarly if you add a database comment to a field, Entity Developer will suggest copying the changed comment to the model, but it does not happen, and will appear as a new change every time you syncronize.
Thank you for the details. We have reproduced both issues. We will notify you when they are fixed.

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

Re: wrong length for nvarchar(max)

Post by Shalex » Thu 22 Dec 2016 18:24

The issues are fixed in the new (6.0.157) build of Entity Developer.

Post Reply