2016-03-04 89 views
0

每次我想在迁移后更新数据库时,都会出现标题中的错误。我的代码如下:无法在更新数据库后附加文件“.mdf”作为数据库'aspnet

Charity类:

public class Charity 
{ 
    public int ID { get; set; } 
    [StringLength(60, MinimumLength = 3, ErrorMessage = "There must be a name")] 
    public string DisplayName { get; set; } 
    [DisplayFormat(DataFormatString = "{dd-MM-yyyy}")] 
    public DateTime Date { get; set; } 
    [Range(2, Int32.MaxValue, ErrorMessage = "The must be greater than £2")] 
    public Double Amount { get; set; } 
    public Double TaxBonus { get; set; } 
    [StringLength(20, MinimumLength = 4, ErrorMessage = "Please leave a comment")] 
    public String Comment { get; set; } 
} 

public class CharityDBContext : DbContext //controls information in database 
{ 
    public DbSet<Charity> Donations { get; set; } //creates a donation database 
} 

web.config

<connectionStrings> 
    <add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-CharitySite-20160222115154.mdf;Initial Catalog=aspnet-CharitySite-20160222115154;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
    <add name="CharityDBContext" 
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Donations.mdf;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
</connectionStrings> 
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
</startup> 

服务器错误:

The model backing the 'CharityDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database

运行程序时它指出首先使用服务器错误迁移,所有步骤都可以完美工作,直到最后一步ch是'update-database'它显示标题中的错误。它要求我执行迁移的原因是因为我在模型中验证了一些东西。

回答

0

您需要更新您的数据库以匹配程序中的模型。 尝试这种在这些命令(在工具 - > NugetPackage管理器 - >包管理器控制台):

  • Add-Migration 'give it a name'命名的东西signofocant给你,像 “updatedmodels” 或类似的东西
  • update-database
0

如果没有启用迁移,在NuGet包管理器控制台,输入:

启用的迁移(运行这只是一次)

附加迁移(使用您更改每次)

更新数据库(使用此应用新的更改)