EFCore-仅当另一个表中不存在种子数据时 [英] EFCore - Seed data only if it doesn't exist in another table

查看:311
本文介绍了EFCore-仅当另一个表中不存在种子数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.net core 3.1中使用EntityFramework播种一些数据,并且遇到一个问题:

I want to seed some data using EntityFramework in .net core 3.1 and I'm facing an issue:

我有两个SQL表(所以有两个DbSet< ;>):

I've got two SQL tables (so two DbSet<>):

public virtual DbSet<TableA> TableA { get; set; }
public virtual DbSet<TableB> TableB { get; set; }

表A具有以下结构:

[Key] 
public int Id { get; set; } // PK
public string EnglishText { get; set; } // some value

表B具有以下结构:

[Key]
public int Id { get; set; } // PK
public int TableAId { get; set; } // FK to Table A
public string TranslatedText { get; set; } // some value

要为表A播种数据,我使用OnModelCreating(ModelBuilder modelBuilder) DBContext中的方法:

For seeding the data for table A, I use the OnModelCreating(ModelBuilder modelBuilder) method in the DBContext:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Seed TableA
        modelBuilder.Entity<TableA>().HasData(
            new TableA { Id = 1, EnglishText = "first data"}
        );
    }

我只想在表B不包含记录的情况下播种通过FK引用TableA,我不确定如何在OnModelCreating方法中做到这一点。

I then want to seed table B only if it doesn't contain a record that reference TableA (via the FK), I'm not sure how to do that in the OnModelCreating method.

我想我正在寻找类似的东西:

I guess I'm after something like:

modelBuilder.Entity<TableB>().HasData(var X = new TableB{...}).Where([X.TableAId is not in TableA])

如果有人有想法或可以指出我的方向,那

If someone has an idea or can point me in a direction, that would be very much appreciated.

推荐答案

我遇到了完全相同的问题。所以我决定扩展 DataBuilder 如下:

I had the exact same problem. So i decided to extend DataBuilder as follow:

using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;

namespace Infrastructure
{
    static class DataBuilderDynamicData
    {
        private static object asyncObject = new object();
        private static Dictionary<Type, List<EntityBase>> dynamicData = new Dictionary<Type, List<EntityBase>>();
        public static DataBuilder<TEntity> HasDynamicData<TEntity>(this EntityTypeBuilder<TEntity> builder, Func<Dictionary<Type, List<EntityBase>>, TEntity> Resolver) where TEntity : EntityBase
        {
            var targetType = typeof(TEntity);
            TEntity model;

            //Note: Not necessary, but just in case several threads reached the same code scope.
            //Note: Lock has some performance hits, but it is negligible due to the fact that Seeding is an In-Design-time process (not run-time).
            lock (asyncObject)
            {
                model = Resolver(dynamicData);
                if (dynamicData.ContainsKey(targetType))
                    dynamicData[targetType].Add(model);
                else
                    dynamicData.Add(targetType, new List<EntityBase> { model });
            }

            return builder.HasData(model);
        }
    }
}  

然后用它代替 HasData()像下面的代码:

Then use it instead of HasData() like following code:

        foreach (var model in models)
        {
            builder.HasDynamicData<ProvinceMap>((dic) =>
            {
                //Please note that "Province" should be already seeded with "HasDynamicData" method. Otherwise, you get an error here.
                var provinces = dic[typeof(Province)];
                return new ProvinceMap
                {
                   Id = 1,
                   ProvinceId = provinces.Cast<Province>().First(item => item.Name=="SOME-PROVINCE"),
                   //Some other seed data.
                };
            });
        }  

请注意, EntityBase 是我的自定义基类。您可以改用 object

Please note that EntityBase is my custom base class. You can use object instead.

public abstract class EntityBase
{
    public int Id { get; set; }
}  

每个实体,例如 ProvinceMap 继承自 EntityBase

您可以通过将relevent配置代码放在使用者类()之前的方式中,以确保字典已经具有所需的基本数据(在我的示例中:Province类):

Every entities e.g. Province and ProvinceMap are inherited from EntityBase.
You can make sure that dictionary already has the base data you want (in my example: Province class) with placing the relevent config code prior to consumer class ()in my example ProvinceMap class:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfiguration<Province>(new ProvinceConfiguration()); //First, declare the base class.
        modelBuilder.ApplyConfiguration<ProvinceMap>(new ProvinceMapConfiguration()); //Next, declare the consumer class.
    }

您可以实现 HasData()方法来满足您的需求。

You can implement other overloads of the HasData() method to fit your needs.

这篇关于EFCore-仅当另一个表中不存在种子数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆