Ideally include a complete code listing that we can run to reproduce the issue.
Alternatively, you can provide a project/solution that we can run.
Describe what is not working as expected.
If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message:
Stack trace:
MySQL version:
Operating system:
Pomelo.EntityFrameworkCore.MySql version:
Microsoft.AspNetCore.App version:
Other details about my project setup:
System.ArgumentException: Missing length for data type "varchar".
at Pomelo.EntityFrameworkCore.MySql.Storage.Internal.MySqlTypeMappingSource.ValidateMapping(CoreTypeMapping mapping, IProperty property)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMappingWithConversion(RelationalTypeMappingInfo& mappingInfo, IReadOnlyList`1 principals)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.FindMapping(IProperty property)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSource.Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMappingSource.FindMapping(IProperty property)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IProperty source, IProperty target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(TableMapping source, TableMapping target, DiffContext diffContext)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace,
String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Missing length for data type "varchar".
Please fill out the issue template, so we can help you. We are unable to help you with the little information you gave us and and no explanation.
Though we still need more information from you, the exception suggests that you are using the MySQL type varchar without specifying its length.
For the type varchar you always need to specifiy the maximum length of the column:
```c#
//
// Using Data Annotations:
//
public class IceCream
{
public string IceCreamId { get; set; }
// This should work:
[Column(TypeName = "varchar(200)")]
public string Name { get; set; }
// This should also work:
[Column(TypeName = "varchar")]
[MaxLength(200)]
public string Brand { get; set; }
}
//
// Using FluentAPI:
//
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity
entity =>
{
// This should work:
entity.Property(e => e.Name)
.HasColumnType("varchar(200)");
// This should also work:
entity.Property(e => e.Brand)
.HasColumnType("varchar")
.HasMaxLength(200);
});
}
```
If you do not want to specify the maximum length, use a MySQL data type that allows a variable length (e.g. longtext).
@khelpix I am closing this issue, because we haven't heard back from you. If this is still something you need help with, please provide us with the missing information. We will reopen this issue then.
This is an issue still when using projections. For example...
// Entity framework entities
public class UserEntity
{
public Guid Uuid { get; set; }
public virtual CompanyEntity Company { get; set; }
}
public class CompanyEntity
{
public Guid Uuid { get; set; }
}
-----------------------------------------------------------------
// User entity mapping
builder
.Property(x => x.Uuid)
.HasColumnType("char(36)");
builder
.HasOne(x => x.Company)
.WithMany(x => x.Users);
-----------------------------------------------------------------
// Company entity mapping
builder
.Property(x => x.Uuid)
.HasColumnType("char(36)");
-----------------------------------------------------------------
// User DTO
public class UserDto
{
public Guid CompanyUuid { get; set; }
}
-----------------------------------------------------------------
// Query
userDbSet
.AsQueryable()
.Select(
x => new UserDto
{
CompanyUuid = x.Company.Uuid
}
)
.ToQueryString();
Although both columns have the column type set, when the query is performed based on the projection, Pomelo throws the following exception Missing length for data type "char". for the CompanyUuid on UserDto. This projection should be possible but I cannot work out how to do it. I don't want to have to add column types or any entity configuration for my DTO as its not a concern of the DTO.
How do you resolve this issue?
I'm using 5.0.0-alpha.2
@mbrookson I am unable to reproduce your issue.
The following code works fine on my end:
Program.cs
```c#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
namespace IssueConsoleTemplate
{
public class UserEntity
{
public Guid Uuid { get; set; }
public Guid CompanyUuid { get; set; }
public virtual CompanyEntity Company { get; set; }
}
public class CompanyEntity
{
public Guid Uuid { get; set; }
public virtual ICollection<UserEntity> Users { get; set; } = new HashSet<UserEntity>();
}
public class Context : DbContext
{
public virtual DbSet<UserEntity> Users { get; set; }
public virtual DbSet<CompanyEntity> Companies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var connectionString = "server=127.0.0.1;port=3306;user=root;password=;database=Issue1050_01";
optionsBuilder.UseMySql(
connectionString,
ServerVersion.AutoDetect(connectionString),
options => options.CharSetBehavior(CharSetBehavior.NeverAppend))
.UseLoggerFactory(
LoggerFactory.Create(
configure => configure
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserEntity>(entity =>
{
entity.HasKey(x => x.Uuid);
entity
.Property(x => x.Uuid)
.HasColumnType("char(36)");
entity
.HasOne(x => x.Company)
.WithMany(x => x.Users)
.HasForeignKey(x => x.CompanyUuid);
entity.HasData(
new UserEntity {Uuid = new Guid("1A7F61A4-E896-4C01-ACE1-5AE532780B8E"), CompanyUuid = new Guid("1B7F61A4-E896-4C01-ACE1-5AE532780B8E")},
new UserEntity {Uuid = new Guid("2A7F61A4-E896-4C01-ACE1-5AE532780B8E"), CompanyUuid = new Guid("1B7F61A4-E896-4C01-ACE1-5AE532780B8E")},
new UserEntity {Uuid = new Guid("3A7F61A4-E896-4C01-ACE1-5AE532780B8E"), CompanyUuid = new Guid("2B7F61A4-E896-4C01-ACE1-5AE532780B8E")});
});
modelBuilder.Entity<CompanyEntity>(entity =>
{
entity.HasKey(x => x.Uuid);
entity
.Property(x => x.Uuid)
.HasColumnType("char(36)");
entity.HasData(
new CompanyEntity {Uuid = new Guid("1B7F61A4-E896-4C01-ACE1-5AE532780B8E")},
new CompanyEntity {Uuid = new Guid("2B7F61A4-E896-4C01-ACE1-5AE532780B8E")});
});
}
}
public class UserDto
{
public Guid CompanyUuid { get; set; }
}
internal static class Program
{
private static void Main(string[] args)
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var userDtos = context.Users
.AsQueryable()
.Select(x => new UserDto {CompanyUuid = x.Company.Uuid})
.ToList();
Trace.Assert(userDtos.Count == 3);
}
}
}
</details>
<details>
<summary>Output</summary>
warn: Microsoft.EntityFrameworkCore.Model.Validation[10400]
Sensitive data logging is enabled. Log entries and exception messages may include sensitive application data; this mode should only be enabled during development.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 5.0.1 initialized 'Context' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: ServerVersion 8.0.21-mysql SensitiveDataLoggingEnabled DetailedErrorsEnabled
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (16ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE DATABASE Issue1050_01;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (97ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE CompanyEntity (
Uuid char(36) NOT NULL,
CONSTRAINT PK_CompanyEntity PRIMARY KEY (Uuid)
);
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (141ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE Users (
Uuid char(36) NOT NULL,
CompanyUuid char(36) NOT NULL,
CONSTRAINT PK_Users PRIMARY KEY (Uuid),
CONSTRAINT FK_Users_CompanyEntity_CompanyUuid FOREIGN KEY (CompanyUuid) REFERENCES CompanyEntity (Uuid) ON DELETE CASCADE
);
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO CompanyEntity (Uuid)
VALUES ('1b7f61a4-e896-4c01-ace1-5ae532780b8e');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (11ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO CompanyEntity (Uuid)
VALUES ('2b7f61a4-e896-4c01-ace1-5ae532780b8e');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (12ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO Users (Uuid, CompanyUuid)
VALUES ('1a7f61a4-e896-4c01-ace1-5ae532780b8e', '1b7f61a4-e896-4c01-ace1-5ae532780b8e');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (14ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO Users (Uuid, CompanyUuid)
VALUES ('2a7f61a4-e896-4c01-ace1-5ae532780b8e', '1b7f61a4-e896-4c01-ace1-5ae532780b8e');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO Users (Uuid, CompanyUuid)
VALUES ('3a7f61a4-e896-4c01-ace1-5ae532780b8e', '2b7f61a4-e896-4c01-ace1-5ae532780b8e');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (74ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE INDEX IX_Users_CompanyUuid ON Users (CompanyUuid);
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (5ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT c.Uuid AS CompanyUuid
FROM Users AS u
INNER JOIN CompanyEntity AS c ON u.CompanyUuid = c.Uuid
```
@lauxjpn You're right! I do apologise, I've got something else causing me that exception. My example above wasn't quite representative as I'm using AutoMapper for the projection, but that also works with the simple example. I'll do some more digging and report back if I find out anything else about my issue! Thanks.
Most helpful comment
@lauxjpn You're right! I do apologise, I've got something else causing me that exception. My example above wasn't quite representative as I'm using AutoMapper for the projection, but that also works with the simple example. I'll do some more digging and report back if I find out anything else about my issue! Thanks.