[英]Cannot attach the file ''.mdf' as database 'aspnet after updating database
每次我要在遷移后更新數據庫時,都會出現標題錯誤。 我的代碼如下:
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>
服務器錯誤:
自創建數據庫以來,支持“ CharityDBContext”上下文的模型已更改。 考慮使用代碼優先遷移來更新數據庫
在運行程序時,它指出服務器錯誤以使用第一次遷移,所有步驟都可以正常工作,直到最后一步是“ update-database”,它在標題中顯示錯誤。 之所以要求我執行遷移,是因為我驗證了模型中的一些內容。
您需要更新數據庫以匹配程序中的模型。 在以下命令中嘗試此操作(在工具-> NugetPackage Manager->程序包管理器控制台下):
Add-Migration 'give it a name'
起個名字”,為您命名,例如“ updatedmodels”之類 update-database
如果您未啟用遷移,請在nuget軟件包管理器控制台中輸入:
enable-migrations(僅運行一次)
添加遷移(每次進行更改時都使用此選項)
更新數據庫(使用它來應用新更改)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.