gpt4 book ai didi

c# - Linux : Any particular way thats better than others when seeding data with EF Core? 上的 Dotnet 核心 1.1

转载 作者:行者123 更新时间:2023-11-30 16:00:08 25 4
gpt4 key购买 nike

我正在考虑只进行一次迁移,并在该迁移中播种数据。我不确定我是否希望播种成为我迁移的一部分,也许有一天我想要一个干净的石板。

我大约一年前在 Windows 上做 asp.net 时有以下实现:

using System.Collections.Generic;
using System.Data.Entity.Validation;
using Mentor.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
/**
* Author: matti
*/
namespace Mentor.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<Mentor.Models.ApplicationDbContext>
{
public Configuration()
{
/*if (System.Diagnostics.Debugger.IsAttached == false)
System.Diagnostics.Debugger.Launch();*/
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = "Mentor.Models.ApplicationDbContext";
}

protected override void Seed(Mentor.Models.ApplicationDbContext context)
{
try
{
var passwordHasher = new PasswordHasher();
User user1 = new User()
{
UserName = "mattinielsen5@hotmail.com",
PasswordHash = passwordHasher.HashPassword("Denherpderp21!"),
FirstName = "Matti andreas",
LastName = "Nielsen",
Age = 24,
ProfileText = "Lorem ipsum dolor sit amet, minimum delicatissimi ad eos, " +
"ne veniam eirmod voluptatibus vel, ne eam facilisi inciderint. " +
"Ex eleifend recteque delicatissimi eos, ut erat posse etiam pri." +
" Ei qui commune vivendum legendos, augue accusata in vim, mei at" +
" bonorum pericula definitionem. Has ornatus aliquando vulputate " +
"at, nonumes docendi in mel. Ne duo recusabo percipitur, et nam " +
"vitae nostrud cotidieque, cibo liber mel te.",
IsMentor = true,
IsMentee = false,
UndefinedInterests = new List<Interest>
{

},
MentorInterests = new List<Interest>
{

},
... blabla alot of entities ...
context.SaveChanges();
}
catch (DbEntityValidationException e)
{
//some error handling
}
}
}
}

所以我想要一个类似种子方法的东西,所以我正在考虑根据一些环境变量(如开发)在 startup.cs 中调用我自己的种子方法。我的问题是,你们是怎么做的 - 或者你们会怎么做??

编辑:

我正在考虑这样做,在创建模型时:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{
//One-to-one
modelBuilder.Entity<Account>().HasOne(a => a.Player).WithOne(p => p.Account).HasForeignKey<Player>(p => p.AccountForeignKey);
modelBuilder.Entity<Group>().HasOne(g => g.Role).WithOne(r => r.Group).HasForeignKey<Role>(r => r.GroupForeignKey);
modelBuilder.Entity<GameEvent>().HasOne(e => e.Event);
modelBuilder.Entity<GameEvent>().HasOne(e => e.Game);
modelBuilder.Entity<TeamEvent>().HasOne(e => e.Event);
modelBuilder.Entity<TeamEvent>().HasOne(e => e.Team);
modelBuilder.Entity<GroupEvent>().HasOne(e => e.Event);
modelBuilder.Entity<GroupEvent>().HasOne(e => e.Group);

//one-to-many
modelBuilder.Entity<Player>().HasMany(p => p.Integrations).WithOne(i => i.Player);
modelBuilder.Entity<Player>().HasMany(p => p.Followers);
modelBuilder.Entity<Player>().HasMany(p => p.Activities).WithOne(a => a.Player);
modelBuilder.Entity<Game>().HasMany(g => g.GameEvents).WithOne(ge => ge.Game);
modelBuilder.Entity<Game>().HasMany(g => g.Teams).WithOne(t => t.Game);
modelBuilder.Entity<Team>().HasMany(t => t.TeamEvents).WithOne(te => te.Team);
modelBuilder.Entity<Group>().HasMany(g => g.GroupEvents);

//many to many
modelBuilder.Entity<PlayerGames>().HasKey(pg => new {pg.PlayerId, pg.GameId});
modelBuilder.Entity<PlayerTeams>().HasKey(pt => new {pt.PlayerId, pt.TeamId});
modelBuilder.Entity<PlayerGroups>().HasKey(pg => new {pg.PlayerId, pg.GroupId});

//discriminator values
modelBuilder.Entity<Event>()
.HasDiscriminator<string>("Type")
.HasValue<GameEvent>("GameEvent")
.HasValue<GroupEvent>("GroupEvent")
.HasValue<TeamEvent>("TeamEvent");

CALLING SEED DATA DOWN HERE, that should be fine???
}

最佳答案

推荐的方法是在 Startup.Configure() 的服务范围内运行播种代码。

是这样的:

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<MyContext>();
context.Database.Migrate();
context.EnsureSeedData();
}

您可以在下面的链接中查看更多详细信息。

Implementing Seeding EF Core 1.0

关于c# - Linux : Any particular way thats better than others when seeding data with EF Core? 上的 Dotnet 核心 1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com