This is the code im working with , and im trying to build many to many relationship between Home and Event tables , but im getting this result :
http://localhost:49477/Homes/Index
CREATE TABLE [dbo].[Events] ( [Id] NVARCHAR (128) NOT NULL, [Image] NVARCHAR (MAX) NULL, [InsertDate] DATETIME NULL, [PublishDate] DATETIME NULL, [ExpiryDate] DATETIME NULL, [ShortDescription1] NVARCHAR (MAX) NULL, CONSTRAINT [PK_dbo.Events] PRIMARY KEY CLUSTERED ([Id] ASC));
CREATE TABLE [dbo].[Homes] ( [Id] NVARCHAR (128) NOT NULL, [LocationID] NVARCHAR (128) NULL, [EventID] NVARCHAR (128) NULL, [HappyHourID] NVARCHAR (128) NULL, [Description] NVARCHAR (MAX) NULL, [FacebookLink] NVARCHAR (MAX) NULL, [InstagramLink] NVARCHAR (MAX) NULL, CONSTRAINT [PK_dbo.Homes] PRIMARY KEY CLUSTERED ([Id] ASC), CONSTRAINT [FK_Homes_Events] FOREIGN KEY ([EventID]) REFERENCES [dbo].[Events] ([Id]), CONSTRAINT [FK_Homes_HappyHours] FOREIGN KEY ([HappyHourID]) REFERENCES [dbo].[HappyHours] ([Id]), CONSTRAINT [FK_Homes_Locations] FOREIGN KEY ([LocationID]) REFERENCES [dbo].[Locations] ([Id]));
Server Error in '/' Application.
Invalid column name 'Home_Id'.
Invalid column name 'Event_Id'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'Home_Id'.
Invalid column name 'Event_Id'.
Source Error:
Line 37:
Line 38: var homes = db.Homes.Include(h => h.Event).Include(h => h.HappyHour).Include(h => h.Location);Line 39: return View(homes.ToList());Line 40:
Line 41: |
Source File: S:\CodeRepository\drenusha_gjurka\CoffeeBarRestaurantMVC\CoffeeBarRestaurantMVC\Controllers\HomesController.cs Line: 39
Stack Trace:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CoffeeBarRestaurantMVC.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CoffeeBarRestaurantMVC;
namespace CoffeeBarRestaurantMVC.Controllers
{
public class HomesController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Homes
public ActionResult Index()
{
var homes = db.Homes.Include(h => h.Event).Include(h => h.HappyHour).Include(h => h.Location);
return View(homes.ToList());
}
// GET: Homes/Details/5
public ActionResult Details(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Home home = db.Homes.Find(id);
if (home == null)
{
return HttpNotFound();
}
return View(home);
}
// GET: Homes/Create
public ActionResult Create()
{
ViewBag.EventID = new SelectList(db.Events, "Id", "ShortDescription1");
ViewBag.HappyHourID = new SelectList(db.HappyHours, "Id", "ShortDescription1");
ViewBag.LocationID = new SelectList(db.Locations, "Id", "Name");
return View();
}
// POST: Homes/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,LocationID,EventID,HappyHourID,Description,FacebookLink,InstagramLink")] Home home)
{
if (ModelState.IsValid)
{
db.Homes.Add(home);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EventID = new SelectList(db.Events, "Id", "ShortDescription1", home.EventID);
ViewBag.HappyHourID = new SelectList(db.HappyHours, "Id", "ShortDescription1", home.HappyHourID);
ViewBag.LocationID = new SelectList(db.Locations, "Id", "Name", home.LocationID);
return View(home);
}
// GET: Homes/Edit/5
public ActionResult Edit(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Home home = db.Homes.Find(id);
if (home == null)
{
return HttpNotFound();
}
ViewBag.EventID = new SelectList(db.Events, "Id", "ShortDescription1", home.EventID);
ViewBag.HappyHourID = new SelectList(db.HappyHours, "Id", "ShortDescription1", home.HappyHourID);
ViewBag.LocationID = new SelectList(db.Locations, "Id", "Name", home.LocationID);
return View(home);
}
// POST: Homes/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,LocationID,EventID,HappyHourID,Description,FacebookLink,InstagramLink")] Home home)
{
if (ModelState.IsValid)
{
db.Entry(home).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EventID = new SelectList(db.Events, "Id", "ShortDescription1", home.EventID);
ViewBag.HappyHourID = new SelectList(db.HappyHours, "Id", "ShortDescription1", home.HappyHourID);
ViewBag.LocationID = new SelectList(db.Locations, "Id", "Name", home.LocationID);
return View(home);
}
// GET: Homes/Delete/5
public ActionResult Delete(string id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Home home = db.Homes.Find(id);
if (home == null)
{
return HttpNotFound();
}
return View(home);
}
// POST: Homes/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(string id)
{
Home home = db.Homes.Find(id);
db.Homes.Remove(home);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
@model IEnumerable<CoffeeBarRestaurantMVC.Models.Home>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}<h2>Index</h2><p>
@Html.ActionLink("Create New", "Create")</p><table class="table"><tr><th>
@Html.DisplayNameFor(model => model.Id)</th><th>
@Html.DisplayNameFor(model => model.Event.Id)</th><th>
@Html.DisplayNameFor(model => model.HappyHour.Id)</th><th>
@Html.DisplayNameFor(model => model.Event.ShortDescription1)</th><th>
@Html.DisplayNameFor(model => model.HappyHour.ShortDescription1)</th><th>
@Html.DisplayNameFor(model => model.Location.Name)</th><th>
@Html.DisplayNameFor(model => model.Description)</th><th>
@Html.DisplayNameFor(model => model.FacebookLink)</th><th>
@Html.DisplayNameFor(model => model.InstagramLink)</th><th></th></tr>
@foreach (var item in Model) {
<tr><td>
@Html.DisplayFor(modelItem => item.Id)</td><td>
@Html.DisplayFor(modelItem => item.Event.Id)</td><td>
@Html.DisplayFor(modelItem => item.HappyHour.Id)</td><td>
@Html.DisplayFor(modelItem => item.Event.ShortDescription1)</td><td>
@Html.DisplayFor(modelItem => item.HappyHour.ShortDescription1)</td><td>
@Html.DisplayFor(modelItem => item.Location.Name)</td><td>
@Html.DisplayFor(modelItem => item.Description)</td><td>
@Html.DisplayFor(modelItem => item.FacebookLink)</td><td>
@Html.DisplayFor(modelItem => item.InstagramLink)</td><td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })</td></tr>
}</table>
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Web;namespace CoffeeBarRestaurantMVC.Models
{ public class Home { public Home() { this.Events = new HashSet<Event>(); // this.Locations = new HashSet<Location>(); // this.HappyHours = new HashSet<HappyHour>(); } public string Id { get; set; } public string LocationID { get; set; } public string EventID { get; set; } public string HappyHourID { get; set; } public string Description { get; set; } public string FacebookLink { get; set; } public string InstagramLink { get; set; } public virtual Event Event { get; set; } public virtual Location Location { get; set; } public virtual HappyHour HappyHour { get; set; } public virtual ICollection<Event> Events { get; set; } // public virtual ICollection<Location> Locations { get; set; } // public virtual ICollection<HappyHour> HappyHours { get; set; } }
}
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations.Schema;using System.Linq;using System.Web;namespace CoffeeBarRestaurantMVC.Models
{ public class Event { public Event() { this.Homes = new HashSet<Home>(); // this.Locations = new HashSet<Location>(); // this.HappyHours = new HashSet<HappyHour>(); } public string Id { get; set; } public string Image { get; set; } public Nullable<System.DateTime> InsertDate { get; set; } public Nullable<System.DateTime> PublishDate { get; set; } public Nullable<System.DateTime> ExpiryDate { get; set; } public string ShortDescription1 { get; set; } // [ForeignKey("Home")] // public string HomeID { get; set; } // public string HomeId { get; set; } // [InverseProperty("Homes")] // public virtual Home Home { get; set; } public virtual ICollection<Home> Homes { get; set; } }
}