我正在尝试使用MVC应用程序在EF6中创建一个简单的外键连接。基本上,我有3张桌子。艺术品,艺术家,艺术类型。艺术品可以有艺术家和艺术类型。艺术家可以拥有多个艺术作品,艺术类型可以有多个艺术作品。
这是我的艺术品模型类
namespace cmackay3_MVC_Arthouse.Models
{
public class Artwork
{
public int ID { get; set; }
[Required(
ErrorMessage = "Field is required")]
[StringLength(255,MinimumLength = 20,
ErrorMessage = "Please enter a name up to 255 caharacters")]
[DisplayName("Name or Title")]
public string Name { get; set; }
[Required(
ErrorMessage = "Field is required")]
[DataType(DataType.Date,
ErrorMessage = "Please enter a valid date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[DisplayName("Date Started")]
public DateTime Started { get; set; }
[DataType(DataType.Date,
ErrorMessage = "Please enter a valid date")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
[DisplayName("Date Finished")]
public DateTime Finished { get; set; }
[Required(
ErrorMessage = "Field is Required")]
[StringLength(511, MinimumLength = 20,
ErrorMessage = "Please enter between 20 and 511 characters")]
public string Description { get; set; }
[Required(
ErrorMessage = "Field is required")]
[Range(1,999999,
ErrorMessage = "Please enter a value between $1 and $999,999")]
[DisplayName("Estimated Value")]
public double Value { get; set; }
[Required(
ErrorMessage = "You must select an artist")]
[DisplayName("Artist")]
public int ArtistID { get; set; }
[Required(
ErrorMessage = "You must select an art type")]
[DisplayName("Art Type")]
public int TypeID { get; set;}
public virtual Artist Artist { get; set; }
public virtual Type Type { get; set; }
正如您所看到的,我将两个虚拟对象作为外键设置在底部。我被告知这些是导航属性。
这是我的Artist模型类
namespace cmackay3_MVC_Arthouse.Models
{
public class Artist
{
[Key]
public int ID { get; set; }
[DisplayName("First Name")]
[Required(
ErrorMessage = "This field is required")]
[StringLength(50,
ErrorMessage = "Please enter a name shorter than 50 characters")]
public string FirstName { get; set; }
[DisplayName("Middle Name")]
[StringLength(30,
ErrorMessage = "Please enter a middle name fewer than 30 characters")]
public string MiddleName { get; set; }
[DisplayName("Last Name")]
[Required(
ErrorMessage = "This field is required")]
[StringLength(50,
ErrorMessage = "Please enter a last name shorter than 50 characters")]
public string LastName { get; set; }
[Required(
ErrorMessage = "This field is required")]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
ErrorMessage = "Please enter a valid phone number")]
public Int64 Phone { get; set; }
[Required(
ErrorMessage = "This field is required")]
public DateTime DOB { get; set; }
[DisplayName("Consignment Rate")]
[Required(
ErrorMessage = "This field is required")]
[Range(10,70,
ErrorMessage = "Please enter a value from 10 to 70")]
public int Rate { get; set; }
public virtual ICollection<Artwork> Artworks { get; set; }
}
}
据我所知,底部的艺术品清单可以容纳艺术家的多件艺术品。
这是最终的模型类,ArtType
namespace cmackay3_MVC_Arthouse.Models
{
public class ArtType
{
[Key]
public int ID { get; set; }
[Required(
ErrorMessage = "This field is Required")]
[StringLength(25,
ErrorMessage = "Please enter a value under 25")]
public string Type { get; set; }
public virtual ICollection<Artwork> Artworks { get; set; }
}
}
这是我的ArtEntities课程
namespace cmackay3_MVC_Arthouse.DAL
{
public class ArtEntities : DbContext
{
public DbSet<Artwork> Artworks { get; set; }
public DbSet<Artist> ArtistItem { get; set; }
public DbSet<ArtType> ArtTypeItem { get; set; }
}
}
现在每当我尝试访问其中一个视图时,我收到一个错误,值不能为空。参数名称:entitySet。
我很丢失,不太确定我做错了什么。非常感谢任何建议。
感谢。
编辑:这是我的艺术品控制器文件。
namespace cmackay3_MVC_Arthouse.Controllers
{
public class ArtworksController : Controller
{
private ArtEntities db = new ArtEntities();
// GET: Artworks
public ActionResult Index()
{
return View(db.Artworks.ToList());
}
// GET: Artworks/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Artwork artwork = db.Artworks.Find(id);
if (artwork == null)
{
return HttpNotFound();
}
return View(artwork);
}
// GET: Artworks/Create
public ActionResult Create()
{
return View();
}
// POST: Artworks/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,Name,Started,Finished,Description,Value,ArtistID,TypeID")] Artwork artwork)
{
if (ModelState.IsValid)
{
db.Artworks.Add(artwork);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(artwork);
}
// GET: Artworks/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Artwork artwork = db.Artworks.Find(id);
if (artwork == null)
{
return HttpNotFound();
}
return View(artwork);
}
// POST: Artworks/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,Name,Started,Finished,Description,Value,ArtistID,TypeID")] Artwork artwork)
{
if (ModelState.IsValid)
{
db.Entry(artwork).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(artwork);
}
// GET: Artworks/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Artwork artwork = db.Artworks.Find(id);
if (artwork == null)
{
return HttpNotFound();
}
return View(artwork);
}
// POST: Artworks/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Artwork artwork = db.Artworks.Find(id);
db.Artworks.Remove(artwork);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}