2using System.ComponentModel.DataAnnotations;
3using System.Linq.Expressions;
4using System.Reflection;
6using Microsoft.EntityFrameworkCore;
25 this ModelBuilder modelBuilder,
26 Expression<Func<TEntity, string?>> expression)
29 var
property = modelBuilder
31 .Property(expression);
33 .HasCharSet(
"utf8mb4");
35 var propertyInfo = GetPropertyFromExpression(expression);
36 var stringLengthAttribute = propertyInfo.GetCustomAttribute<StringLengthAttribute>();
39 property.HasColumnType(
"longtext");
52 MemberExpression memberExpression;
55 if (expression.Body is UnaryExpression unaryExpression)
56 if (unaryExpression.Operand is MemberExpression unaryAsMember)
57 memberExpression = unaryAsMember;
59 throw new ArgumentException(
"Cannot get property from expression!", nameof(expression));
60 else if (expression.Body is MemberExpression)
61 memberExpression = (MemberExpression)expression.Body;
63 throw new ArgumentException(
"Cannot get property from expression!", nameof(expression));
65 return (PropertyInfo)memberExpression.Member;
Sanity limits to prevent users from overloading.
const int MaximumStringLength
Length limit for strings in fields.
Extension methods for the ModelBuilder class.
static ModelBuilder MapMySqlTextField< TEntity >(this ModelBuilder modelBuilder, Expression< Func< TEntity, string?> > expression)
Set a given TEntity 's property's column charset to "utf8mb4". Only for use with the MySQL/MariaDB pr...
static PropertyInfo GetPropertyFromExpression< TEntity >(Expression< Func< TEntity, string?> > expression)
Get the PropertyInfo pointed to by an expression .