Skip to content

Instantly share code, notes, and snippets.

@giridharprakash
Created February 12, 2020 15:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giridharprakash/000dd73a290e3275bdcdc35c3fdb3f8c to your computer and use it in GitHub Desktop.
Save giridharprakash/000dd73a290e3275bdcdc35c3fdb3f8c to your computer and use it in GitHub Desktop.
public static IServiceCollection InitDatabaseContext(this IServiceCollection services, string connectionString)
{
services.AddDbContext<AppDbContext>(opts =>
{
opts.UseSqlServer(connectionString);
opts.ReplaceService<IConventionSetBuilder, CustomSetBuilder>();
});
return services;
}
public class CustomSetBuilder : SqlServerConventionSetBuilder
{
public CustomSetBuilder(RelationalConventionSetBuilderDependencies dependencies, ISqlGenerationHelper sqlGenerationHelper) : base(dependencies, sqlGenerationHelper)
{
}
public override ConventionSet AddConventions(ConventionSet conventionSet)
{
var et = conventionSet.ForeignKeyAddedConventions.FirstOrDefault(f => f is ForeignKeyIndexConvention);
if (et != null)
conventionSet.ForeignKeyAddedConventions.Remove(et);
return base.AddConventions(conventionSet);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment