码迷,mamicode.com
首页 > 其他好文 > 详细

EFCore 迁移命令移除外键

时间:2020-08-14 23:25:16      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:only   ssm   not   ppi   app   ret   color   dep   table   

 

继承 MigrationsModelDiffer,重载 GetDifferences 并移除 ForeignKeys

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<挂起>")]
    public class MigrationsModelDifferWithoutForeignKey : MigrationsModelDiffer
    {
        public MigrationsModelDifferWithoutForeignKey
            ([NotNull] IRelationalTypeMappingSource typeMappingSource,
            [NotNull] IMigrationsAnnotationProvider migrationsAnnotations,
            [NotNull] IChangeDetector changeDetector,
            [NotNull] IUpdateAdapterFactory updateAdapterFactory,
            [NotNull] CommandBatchPreparerDependencies commandBatchPreparerDependencies)
            : base(typeMappingSource, migrationsAnnotations, changeDetector, updateAdapterFactory, commandBatchPreparerDependencies)
        {
        }

        public override IReadOnlyList<MigrationOperation> GetDifferences(IModel source, IModel target)
        {
            var operations = base.GetDifferences(source, target);

            foreach (var operation in operations.OfType<CreateTableOperation>())
                operation.ForeignKeys?.Clear();

            return operations;
        }
    }

 

使用时,替换服务即可:

  services.AddDbContext<MyDbContext>(options =>
  {
    options.UseSqlServer(Default);
    options.ReplaceService<IMigrationsModelDiffer, MigrationsModelDifferWithoutForeignKey>();
  });

 

EFCore 迁移命令移除外键

标签:only   ssm   not   ppi   app   ret   color   dep   table   

原文地址:https://www.cnblogs.com/lludcmmcdull/p/13502567.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!