use*_*273 21 c# asp.net migration asp.net-mvc asp.net-mvc-4
我正在使用Visual Studio 2012中的MVC4进行项目,并在表中添加了一列.
现在,当我想调试我的项目时,错误说使用迁移来更新我的数据库.
我该怎么办?
我一直在搜索,发现了一些方法,如:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<ResTabelaIndex>(null);
}
Run Code Online (Sandbox Code Playgroud)
但不知道如何和在哪里实施这个......曾试图app_start,global.asax等...
我发现的是,从nuget直接在控制台中启用迁移.
但我无法做到这一点.
我使用的命令:
Enable-Migrations -EnableAutomaticMigrations
Run Code Online (Sandbox Code Playgroud)
==>控制台说找到了多个上下文.为了能够使用,Enable-Migrations -ContextTypeName NameOfTheNamespace.Models.DefaultConnection
但我不知道是什么-ContextTypeName,已经尝试了很多但无法理解.
My Model Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Migrations;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.Infrastructure;
namespace Vista.Models
{
public class TabelaIndex
{
public int ID { get; set; }
public string n_empresa { get; set; }
public string titulo{ get; set; }
public string url { get; set; }
public string imagens { get; set; }
}
public class DefaultConnection : DbContext
{
public DbSet<TabelaIndex> ResTabelaIndex { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
Pat*_*lor 28
命令:
enable-migrations default contextadd-migration InitialCreate (用于生成快照)add-migration InitialCreate (应用快照)update-databaseupdate-database -verbose详细的探索将在这里:http: //www.dotnet-tricks.com/Tutorial/entityframework/R54K181213-Understanding-Entity-Framework-Code-First-Migrations.html
SOf*_*tic 26
错误是说你有两个上下文.当您首次使用MVC 4创建项目时,Visual Studio SimpleMembership默认为您创建一个上下文(请检查Models/Account.cs)或执行ctrl+ffor UsersContext,如果您不使用,则可以删除此文件SimpleMembership.删除此上下文后,继续并将以下内容添加到您的DefaultConnection班级:
protected override void OnModelCreating(DbModelBuilder builder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DefaultConnection,Configuration>());
}
Run Code Online (Sandbox Code Playgroud)
如果您正确启用了迁移,那么您还应该有一个名为的文件夹Migrations,其中包含一个Configuration类,其构造函数应如下所示(如果要启用自动迁移):
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
83845 次 |
| 最近记录: |