Created
September 29, 2016 19:39
-
-
Save mjrousos/60682be4c14f05c9e3094a97a2143be4 to your computer and use it in GitHub Desktop.
EF6 -> EF.Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) | |
{ | |
var context = serviceScope.ServiceProvider.GetService<MyContext>(); | |
if (context.Database.EnsureCreated()) | |
{ | |
context.SeedData(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})"))); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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