Entity framework 如何向EntityFramework 7项目添加迁移
我正在使用asp.net 5和EF 7 VS2015启动一个新项目 我使用用户管理选择了项目模板。 现在,我想向dbContext添加一些类,并用我的新类创建一个新模式 这是我的ApplicationDbContext的外观:Entity framework 如何向EntityFramework 7项目添加迁移,entity-framework,entity-framework-migrations,asp.net-core,entity-framework-core,Entity Framework,Entity Framework Migrations,Asp.net Core,Entity Framework Core,我正在使用asp.net 5和EF 7 VS2015启动一个新项目 我使用用户管理选择了项目模板。 现在,我想向dbContext添加一些类,并用我的新类创建一个新模式 这是我的ApplicationDbContext的外观: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public DbSet<Candidate> Candidates { get; s
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Candidate> Candidates { get; set; }
public DbSet<Manager> Managers { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.Entity<Candidate>().Key(x => x.Id);
builder.Entity<Manager>().Key(x => x.Id);
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共DbSet候选者{get;set;}
公共数据库集管理器{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//自定义ASP.NET标识模型,并根据需要覆盖默认值。
//例如,可以重命名ASP.NET标识表名称等。
//在调用base.OnModelCreating(builder)后添加自定义项;
builder.Entity().Key(x=>x.Id);
builder.Entity().Key(x=>x.Id);
}
}
我无法重新创建数据库或将数据库迁移到具有我的候选者
和管理者
表的版本
我必须输入哪些命令才能显示我的数据库?我的朋友谷歌(Google)和必应(Bing)向我指出了各个方向,但我发现没有一个方向是有效的。你需要使用新的
dnx
命令,例如:
dnx . ef migration add NameOfMigration
要运行迁移,请执行以下操作:
dnx . ef migration apply
您需要使用新的
dnx
命令,例如:
dnx . ef migration add NameOfMigration
要运行迁移,请执行以下操作:
dnx . ef migration apply
我发现这显示了如何在ASP.NET5项目上处理迁移,但总而言之,您需要应用@DavidG在其回答中建议的命令
我知道这不是您的情况,但如果您使用的是类库项目,则需要运行以下命令:
打开包管理器控制台:
- 运行
如果是第一次,它将为您的模型创建初始表集而构建一个迁移,否则它将基于您自上次创建迁移以来对模型所做的更改构建下一次迁移addmigration MigrationName
- 运行
将新迁移应用于数据库。 如果您的数据库尚不存在,将为您创建该数据库 在应用迁移之前Apply Migration
DbContext
类或AddDbContext
方法中重写onconfigurang
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
}
我发现这显示了如何在ASP.NET5项目上处理迁移,但总而言之,您需要应用@DavidG在其回答中建议的命令
我知道这不是您的情况,但如果您使用的是类库项目,则需要运行以下命令:
打开包管理器控制台:
- 运行
如果是第一次,它将为您的模型创建初始表集而构建一个迁移,否则它将基于您自上次创建迁移以来对模型所做的更改构建下一次迁移addmigration MigrationName
- 运行
将新迁移应用于数据库。 如果您的数据库尚不存在,将为您创建该数据库 在应用迁移之前Apply Migration
DbContext
类或AddDbContext
方法中重写onconfigurang
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp;Trusted_Connection=True;");
}
根据ASP.NET 5和Entity 7 RC1,步骤如下:
project.json
中,您应该可以看到这种依赖关系
“EntityFramework.Commands”:“7.0.0-rc1-final”
project.json
中,您将拥有以下ef
命令:
“命令”:{
“ef”:“EntityFramework.Commands”,
“web”:“Microsoft.AspNet.Server.Kestrel”
}
dnx . ef migration add NameOfMigration
D:\Projects\MyProject\src\MyProject.Web.Api>dnx ef迁移添加初始--targetProject MyProject.Data.SqlServer
ef
将在项目中创建的迁移,并使用以下命令将更改应用到数据库(为项目配置的数据库):
D:\Projects\MyProject\src\MyProject.Web.Api>dnx ef数据库更新
--targetProject
允许您指定DbContext所在的项目以及创建文件夹迁移的位置。如果DbContext位于主项目中,则可以忽略它(但我建议为所有与持久性相关的内容提供一个类库项目,这样这个命令就很方便了)
在您的Startup.cs
中,您通常会对实体进行配置,包括连接字符串。这是一个基本的例子:
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<KuneDbContext>(options => {
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
});
// Add identity here http://docs.asp.net/en/latest/tutorials/your-first-aspnet-application.html
services.AddMvc();
// Add application services
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<KuneDbContext>()
.Database.Migrate();
}
}
catch { }
}
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
//Seed Data if you want to load some test data in the DB
//SeedData.Initialize(app.ApplicationServices);
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
公共启动(IHostingEnvironment环境)
{
//设置配置源。
var builder=new ConfigurationBuilder()
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=builder.Build();
builder.AddEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;private set;}
//此方法由运行时调用。使用此方法向容器中添加服务。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddEntityFramework()
.AddSqlServer()文件
添加