简体   繁体   English

更新数据库后,无法将文件“ .mdf”附加为数据库“ aspnet”

[英]Cannot attach the file ''.mdf' as database 'aspnet after updating database

Every time I want to update database after migration the error in the title occurs. 每次我要在迁移后更新数据库时,都会出现标题错误。 My code is below: 我的代码如下:

Charity class: 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 : 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>

Server error: 服务器错误:

The model backing the 'CharityDBContext' context has changed since the database was created. 自创建数据库以来,支持“ CharityDBContext”上下文的模型已更改。 Consider using Code First Migrations to update the database 考虑使用代码优先迁移来更新数据库

When running the program it states the server error to use first migration, all the steps work perfectly until the very last step which is 'update-database' it displays the error in the title. 在运行程序时,它指出服务器错误以使用第一次迁移,所有步骤都可以正常工作,直到最后一步是“ update-database”,它在标题中显示错误。 The reason it asks me to perform migration is because I validated a few things in the model. 之所以要求我执行迁移,是因为我验证了模型中的一些内容。

You need to update your DB to match the models in your program. 您需要更新数据库以匹配程序中的模型。 try this in these commands( under tools ->NugetPackage Manager -> package manager console) : 在以下命令中尝试此操作(在工具-> NugetPackage Manager->程序包管理器控制台下):

  • Add-Migration 'give it a name' name it something signofocant to you, something like "updatedmodels" or something like that Add-Migration 'give it a name'起个名字”,为您命名,例如“ updatedmodels”之类
  • update-database

If you did not enable migrations, in the nuget package manager console, type: 如果您未启用迁移,请在nuget软件包管理器控制台中输入:

enable-migrations (run this just once) enable-migrations(仅运行一次)

add-migration (use this every time you make a change) 添加迁移(每次进行更改时都使用此选项)

update-database (use this to apply the new changes) 更新数据库(使用它来应用新更改)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM