【发布时间】:2019-01-03 01:55:57
【问题描述】:
我在尝试修改 MVC 应用程序中的 Delete 方法时收到此错误。
错误 1 'ContactManager.Models.ContactManagerDBEntities2' 没有 包含“ApplyPropertyChanges”的定义并且没有扩展名 方法“ApplyPropertyChanges”接受类型的第一个参数 'ContactManager.Models.ContactManagerDBEntities2' 可以找到(是 您缺少 using 指令或程序集 参考?) c:\users\sp_admin\documents\visual studio 2013\Projects\ContactManager\ContactManager\Controllers\HomeController.cs 123 25 ContactManager
错误 2“ContactManager.Models.Contact”不包含定义 对于 'EntityKey' 并且没有扩展方法 'EntityKey' 接受第一个 可以找到“ContactManager.Models.Contact”类型的参数(是 您缺少 using 指令或程序集 参考?) c:\users\sp_admin\documents\visual studio 2013\Projects\ContactManager\ContactManager\Controllers\HomeController.cs 123 62 ContactManager
这里是 HomeControllers.cs
// GET: /Home/Delete/5
public ActionResult Delete(int id)
{
var contactToDelete = (from c in _entities.Contacts
where c.Id == id
select c).FirstOrDefault();
return View(contactToDelete);
}
//
// POST: /Home/Delete/5
[HttpPost]
/* public ActionResult Delete(int id, FormCollection collection) */
public ActionResult Delete(Contact contactToDelete)
{
if (!ModelState.IsValid)
return View();
try
{
var originalContact = (from c in _entities.Contacts
where c.Id == contactToDelete.Id
select c).FirstOrDefault();
_entities.ApplyPropertyChanges(originalContact.EntityKey.EntitySetName,
contactToDelete); <== here is the code in error ApplyPropertyChanges and
EntityKey.
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
这是 ContactManagerModel.Context.cs 我认为这其中缺少一些东西??但是这个 是从模板生成的。
namespace ContactManager.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ContactManagerDBEntities2 : DbContext
{
public ContactManagerDBEntities2()
: base("name=ContactManagerDBEntities2")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Contact> Contacts { get; set; }
}
}
Contact.cs
namespace ContactManager.Models
{
using System;
using System.Collections.Generic;
public partial class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
}
}
知道由于此代码是从模板生成的,可能会出现什么问题吗? 谢谢
【问题讨论】:
标签: c# asp.net-mvc visual-studio-2012