Skip to content

Instantly share code, notes, and snippets.

@mjrousos
Created September 29, 2016 19:39
Show Gist options
  • Save mjrousos/60682be4c14f05c9e3094a97a2143be4 to your computer and use it in GitHub Desktop.
Save mjrousos/60682be4c14f05c9e3094a97a2143be4 to your computer and use it in GitHub Desktop.
EF6 -> EF.Core
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<MyContext>();
if (context.Database.EnsureCreated())
{
context.SeedData();
}
}
public class IdentifierConvention : IStoreModelConvention<EdmProperty>
{
public void Apply(EdmProperty item, DbModel model)
{
if (item.Name.Length > 30)
{
throw new InvalidOperationException("Column name is greater than 30 characters - " + item.Name);
}
}
}
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore.Storage;
public class MyValidator : RelationalModelValidator
{
const int MAX_TABLE_NAME = 30;
const int MAX_COLUMN_NAME = 30;
public MyValidator(
ILogger<RelationalModelValidator> loggerFactory,
IRelationalAnnotationProvider relationalExtensions,
IRelationalTypeMapper typeMapper)
: base(loggerFactory, relationalExtensions, typeMapper)
{ }
public override void Validate(IModel model)
{
base.Validate(model);
var longTables = model.GetEntityTypes()
.Where(e => e.Relational().TableName.Length > MAX_TABLE_NAME)
.ToList();
if (longTables.Any())
{
throw new NotSupportedException(
$"The following types are mapped to table names that exceed {MAX_TABLE_NAME} characters; "
+ string.Join(", ", longTables.Select(e => $"{e.ClrType.Name} ({e.Relational().TableName})")));
}
var longColumns = model.GetEntityTypes()
.SelectMany(e => e.GetProperties())
.Where(p => p.Relational().ColumnName.Length > MAX_COLUMN_NAME)
.ToList();
if (longColumns.Any())
{
throw new NotSupportedException(
$"The following properties are mapped to column names that exceed {MAX_COLUMN_NAME} characters; "
+ string.Join(", ", longColumns.Select(p => $"{p.DeclaringEntityType.Name}.{p.Name} ({p.Relational().ColumnName})")));
}
}
}
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
public class StateListener : IEntityStateListener
{
public void StateChanging(InternalEntityEntry entry, EntityState newState)
{
if (newState == EntityState.Added)
{
//modify entry.Entity here
}
}
public void StateChanged(InternalEntityEntry entry, EntityState oldState, bool skipInitialFixup, bool fromQuery)
{
}
}
using Microsoft.Extensions.DependencyInjection;
public class MyContext : DbContext
{
private static readonly IServiceProvider _serviceProvider
= new ServiceCollection()
.AddEntityFrameworkSqlServer()
.AddSingleton<IEntityStateListener>(new StateListener())
.AddScoped<RelationalModelValidator, MyValidator>()
.BuildServiceProvider();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseInternalServiceProvider(_serviceProvider)
.UseSqlServer(@"Server = (localdb)\mssqllocaldb;Database=MyDb;Trusted_Connection=True;");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment