Code First 迁移和 SQL Server Management Studio 的问题
Problems with Code First Migration and SQL Server Management Studio
我正在开发的应用程序的数据库出现问题。我有两个 类 - 房屋和捐赠。捐赠需要有与之关联的房屋。
public class House
{
[Key]
public int HouseId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
}
public class Donation
{
[Key]
public int DonationId { get; set; }
public string TypeOfDonation { get; set; }
public decimal Amount { get; set; }
[ForeignKey("Church")]
public int ChurchId { get; set; }
[ForeignKey("House")]
public int HouseId { get; set; }
public virtual House House { get; set; }
public virtual Church Church { get; set; }
}
我已经多次使用 Code First Migrations 并且它有效,但我遇到了两个错误。我注意到的第一个是 SQL Server Studio with Donation。它为所有这些都提供了无效的列名
然后,当我 运行 应用程序并尝试将 Donation 与 House 相关联时,我收到此错误 INSERT 语句与 FOREIGN KEY 约束冲突 "FK_dbo.Donation_dbo.House_HouseId"。冲突发生在数据库 "ChurchDatabase"、table "dbo.House"、列 'HouseId'”。
这是在我的 DonationController 中创建时出现的错误:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "DonationId,HouseId,TypeOfDonation,Amount,ChurchId")] Donation donation)
{
if (ModelState.IsValid)
{
db.Donations.Add(donation);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ChurchId = new SelectList(db.Churches, "ChurchId", "Name", donation.ChurchId);
return View(donation);
}
有人知道发生了什么事吗?它一直在破坏我的头脑
找到我的问题,它在我的视图中:
<div class="form-group">
@Html.LabelFor(model => model.House.HouseId, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.House.HouseId, new { id = "HouseId" })
@Html.ValidationMessageFor(model => model.House.HouseId, "", new { @class = "text-danger" })
</div>
</div>
它需要是:
<div class="form-group">
@Html.LabelFor(model => model.HouseId, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.HouseId, new { id = "HouseId" })
@Html.ValidationMessageFor(model => model.HouseId, "", new { @class = "text-danger" })
</div>
</div>
当它不需要房子位时,我愚蠢地放下了 House.HouseId
我正在开发的应用程序的数据库出现问题。我有两个 类 - 房屋和捐赠。捐赠需要有与之关联的房屋。
public class House
{
[Key]
public int HouseId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
}
public class Donation
{
[Key]
public int DonationId { get; set; }
public string TypeOfDonation { get; set; }
public decimal Amount { get; set; }
[ForeignKey("Church")]
public int ChurchId { get; set; }
[ForeignKey("House")]
public int HouseId { get; set; }
public virtual House House { get; set; }
public virtual Church Church { get; set; }
}
我已经多次使用 Code First Migrations 并且它有效,但我遇到了两个错误。我注意到的第一个是 SQL Server Studio with Donation。它为所有这些都提供了无效的列名
然后,当我 运行 应用程序并尝试将 Donation 与 House 相关联时,我收到此错误 INSERT 语句与 FOREIGN KEY 约束冲突 "FK_dbo.Donation_dbo.House_HouseId"。冲突发生在数据库 "ChurchDatabase"、table "dbo.House"、列 'HouseId'”。
这是在我的 DonationController 中创建时出现的错误:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "DonationId,HouseId,TypeOfDonation,Amount,ChurchId")] Donation donation)
{
if (ModelState.IsValid)
{
db.Donations.Add(donation);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ChurchId = new SelectList(db.Churches, "ChurchId", "Name", donation.ChurchId);
return View(donation);
}
有人知道发生了什么事吗?它一直在破坏我的头脑
找到我的问题,它在我的视图中:
<div class="form-group">
@Html.LabelFor(model => model.House.HouseId, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.House.HouseId, new { id = "HouseId" })
@Html.ValidationMessageFor(model => model.House.HouseId, "", new { @class = "text-danger" })
</div>
</div>
它需要是:
<div class="form-group">
@Html.LabelFor(model => model.HouseId, "Address", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.HouseId, new { id = "HouseId" })
@Html.ValidationMessageFor(model => model.HouseId, "", new { @class = "text-danger" })
</div>
</div>
当它不需要房子位时,我愚蠢地放下了 House.HouseId