我正在使用实体框架创建数据库,我创建了第一个实体作为carlot,第二个汽车将列出相应批次中的汽车。我一直在想我的模型有问题...这里是关注模型
namespace CarLot.Models
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
public class CarModel : DbContext
{
public CarModel()
: base("name=CarModel")
{
}
// Add a DbSet for each entity type that you want to include in your model. For more information
// on configuring and using a Code First model, see http://go.microsoft.com/fwlink/?LinkId=390109.
public DbSet<CarLot> CarLots { get; set; }
public DbSet<Car> Cars { get; set; }
}
public class CarLot
{
//Primary Key
public int CarLotID { get; set; }
public string Name { get; set; }
public string Section { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}
public class Car
{
public int CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int Price { get; set; }
public bool isNew { get; set; }
//foreign key
public int CarLotID { get; set; }
public virtual CarLot Carlot { get; set; }
}
}
我继续为Cars设计控制台,但没有显示任何错误。 但不是我的carlot,这是以下的CarLot文件......它给我带有Carlot的CS0118错误,如下所述,6跟随CarLot给我CS0118,其中指出&#34; CarLot是一个命名空间但是用作类型&# 34;
sing 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 CarLot.Models;
namespace CarLot.Controllers
{
public class CarLotsController : Controller
{
private CarModel db = new CarModel();
// GET: CarLots
public ActionResult Index()
{
return View(db.CarLots.ToList());
}
// GET: CarLots/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
// It is giving me the CS0118 error with the following CarLot
CarLot carLot = db.CarLots.Find(id);
if (carLot == null)
{
return HttpNotFound();
}
return View(carLot);
}
// GET: CarLots/Create
public ActionResult Create()
{
return View();
}
// POST: CarLots/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 = "CarLotID,Name,Section")] CarLot carLot)
{
if (ModelState.IsValid)
{
db.CarLots.Add(carLot);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(carLot);
}
// GET: CarLots/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CarLot carLot = db.CarLots.Find(id);
if (carLot == null)
{
return HttpNotFound();
}
return View(carLot);
}
// POST: CarLots/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 = "CarLotID,Name,Section")] CarLot carLot)
{
if (ModelState.IsValid)
{
db.Entry(carLot).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(carLot);
}
// GET: CarLots/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CarLot carLot = db.CarLots.Find(id);
if (carLot == null)
{
return HttpNotFound();
}
return View(carLot);
}
// POST: CarLots/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
CarLot carLot = db.CarLots.Find(id);
db.CarLots.Remove(carLot);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
答案 0 :(得分:0)
namespace CarLot.Models
{
public class CarLot
{
问题与此代码有关,命名空间和类名都是CarLot 尝试更改其中一个