每次我想在迁移后更新数据库时,都会出现标题中的错误。我的代码如下:无法在更新数据库后附加文件“.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'它显示标题中的错误。它要求我执行迁移的原因是因为我在模型中验证了一些东西。