简体   繁体   中英

Problems with Code First Migration and SQL Server Management Studio

Having a problem with my database for an application I'm developing. I have two classes - House and Donation. A Donation needs to have a House associated with it.

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; }

}

I've used Code First Migrations multiple times and its worked but I'm getting two errors. The first one I've noticed is on SQL Server Studio with Donation. It comes up with Invalid Column Name for all of them

捐赠数据库错误

Then, when I run the application and attempt to associate a Donation with a House, I get this error The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Donation_dbo.House_HouseId". The conflict occurred in database "ChurchDatabase", table "dbo.House", column 'HouseId'".

Here's the Create in my DonationController where I get that error:

 [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);
    }

Anyone have any idea what is going on? Its been wrecking my head

Found my problem, it was in my View:

<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>

It needed to be:

<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>

I was stupidly putting down House.HouseId when it didn't need the House bit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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