-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fluent API with fixed length varchar #1058
Comments
See maximum length documentation for Entity Properties:
|
@Toemsel In addition to what @mguinness said, also be aware that But changing it from If you want to validate your data first, here are some articles about it: If you just want to truncate strings before sending them to the database, it might be enough for you to use a Value Converter, that just truncates client-side strings: entity
.Property(t => t.Name)
.HasMaxLength(32)
.IsRequired()
.HasConversion(
v => v.Substring(0, Math.Min(v.Length, 32)),
v => v); If you want to automate this, based on whether using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Storage;
namespace IssueConsoleTemplate
{
public class IceCream
{
public int IceCreamId { get; set; }
public string Name { get; set; }
}
public class Context : DbContext
{
public DbSet<IceCream> IceCreams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseMySql(
"server=127.0.0.1;port=3306;user=root;password=;database=Issue1058",
b => b.ServerVersion(new ServerVersion("8.0.20-mysql")))
.UseLoggerFactory(
LoggerFactory.Create(
b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IceCream>(
entity =>
{
entity.Property(e => e.Name)
.HasMaxLength(5)
.IsRequired();
});
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
var maxLength = property.GetMaxLength().GetValueOrDefault();
if (maxLength > 0)
{
property.SetValueConverter(new ValueConverter<string,string>(
v => v.Substring(0, Math.Min(v.Length, maxLength)),
v => v));
}
}
}
}
}
internal class Program
{
private static void Main()
{
using (var context = new Context())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
using (var context = new Context())
{
context.IceCreams.Add(new IceCream {Name = "Vanilla"});
context.SaveChanges();
}
using (var context = new Context())
{
var iceCreams = context.IceCreams.ToList();
Debug.Assert(iceCreams.Count == 1);
Debug.Assert(iceCreams[0].Name == "Vanil");
}
}
}
} |
@mguinness oh, well, I thought fluent/annotations would not only exist for validation/creation/migration only. Nice to know and thanks for the hint. @lauxjpn Thanks for letting me know about the value converter. That helps a lot! PS: Yes, varchar is dynamic from 0-32 in my case. It was a poor choice of words. Thank you guys! |
Steps to reproduce
Tag Model
DbContext
The issue
It doesn't matter how I do define the "name" column with the fluet API, the insert won't trim the property. I did try:
the modeldef gets called flawlessly.
Further technical details
MySQL version:
Operating system:
Pomelo.EntityFrameworkCore.MySql version: 3.1.1
Microsoft.AspNetCore.App version: .NET Core 3.1
The text was updated successfully, but these errors were encountered: