gpt4 book ai didi

entity-framework-core - 一个种子 Entity Framework 7 数据库如何?

转载 作者:行者123 更新时间:2023-12-04 02:12:27 26 4
gpt4 key购买 nike

有没有合适的方法来为数据库做种?

我似乎找不到任何文档来完成此任务。

最佳答案

目前,您必须手动播种(但将来有 talk of a high level API 用于此)。

您可以在 Startup 类的 Configure 方法中这样做(假设您使用的是 ASP.NET 5):

public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory logger)
{
using (var context = (MyContext) app.ApplicationServices.GetService<MyContext>())
{
if (env.IsDevelopment())
{
//Add seed code here

context.MyEntity.Add(new MyEntity{ Id = 1 });
//etc

context.SaveChanges();
}
}
}
}

您还可以查看 Music Store示例应用程序 SampleData类更复杂、更健壮。

关于entity-framework-core - 一个种子 Entity Framework 7 数据库如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31612728/

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