new {Id = 1, MakeId = 1, Color = "Black", PetName = "Zippy"},

  new {Id = 2, MakeId = 2, Color = "Rust", PetName = "Rusty"},

  new {Id = 3, MakeId = 3, Color = "Black", PetName = "Mel"},

  new {Id = 4, MakeId = 4, Color = "Yellow", PetName = "Clunker"},

  new {Id = 5, MakeId = 5, Color = "Black", PetName = "Bimmer"},

  new {Id = 6, MakeId = 5, Color = "Green", PetName = "Hank"},

  new {Id = 7, MakeId = 5, Color = "Pink", PetName = "Pinky"},

  new {Id = 8, MakeId = 6, Color = "Black", PetName = "Pete"},

  new {Id = 9, MakeId = 4, Color = "Brown", PetName = "Brownie"},

  new {Id = 10, MakeId = 1, Color = "Rust", PetName = "Lemon",

                                              IsDrivable = false},

};

public static List Orders => new

{

  new {Id = 1, CustomerId = 1, CarId = 5},

  new {Id = 2, CustomerId = 2, CarId = 1},

  new {Id = 3, CustomerId = 3, CarId = 4},

  new {Id = 4, CustomerId = 4, CarId = 7},

  new {Id = 5, CustomerId = 5, CarId = 10},

};

public static List CreditRisks => new

{

  new

  {

    Id = 1,

    CustomerId = Customers[4].Id,

    PersonalInformation = new

    {

      FirstName = Customers[4].PersonalInformation.FirstName,

      LastName = Customers[4].PersonalInformation.LastName

    }

  }

};

<p id="AutBody_Root1011"><strong>Загрузка выборочных данных</strong></p>

Внутренний метод SeedData в классе SampleDatalnitializer добавляет данные из методов класса SampleData к экземпляру ApplicationDbContext и сохраняет данные в базе данных:

internal static void SeedData(ApplicationDbContext context)

{

  try

  {

    ProcessInsert(context, context.Customers!, SampleData.Customers);

    ProcessInsert(context, context.Makes!, SampleData.Makes);

    ProcessInsert(context, context.Cars!, SampleData.Inventory);

    ProcessInsert(context, context.Orders!, SampleData.Orders);

    ProcessInsert(context, context.CreditRisks!, SampleData.CreditRisks);

  }

  catch (Exception ex)

  {

    Console.WriteLine(ex);

    // Поместить сюда точку останова, чтобы выяснить,

    // в чем заключается проблема.

    throw;

  }

  static void ProcessInsert(

    ApplicationDbContext context,

    DbSet table,

    List records) where TEntity : BaseEntity

  {

     if (table.Any)

     {

       return;

     }

    IExecutionStrategy strategy = context.Database.CreateExecutionStrategy;

    strategy.Execute( =>

    {

      using var transaction = context.Database.BeginTransaction;

      try

      {

        var metaData = context.Model.FindEntityType(typeof(TEntity).FullName);

        context.Database.ExecuteSqlRaw(

            $"SET IDENTITY_INSERT {metaData.GetSchema}.

            {metaData.GetTableName} ON");

        table.AddRange(records);

        context.SaveChanges;

        context.Database.ExecuteSqlRaw(

            $"SET IDENTITY_INSERT {metaData.GetSchema}.

            {metaData.GetTableName} OFF");

        transaction.Commit;

      }

      catch (Exception)

      {

        transaction.Rollback;

      }

      });

  }

}

Перейти на страницу:

Похожие книги