2014-02-18 34 views
0

當我嘗試修改MVC應用程序中的Delete方法時出現此錯誤。ApplyPropertyChanges不包含使用MVC的定義

錯誤1「ContactManager.Models.ContactManagerDBEntities2」不 包含一個定義爲「ApplyPropertyChanges」和沒有擴展 方法「ApplyPropertyChanges」接受型 「ContactManager.Models.ContactManagerDBEntities2」的第一個參數可以發現(是 是否缺少using指令或程序集 引用?)C:\用戶\ sp_admin \文件\視覺工作室 2013 \項目\的ContactManager \的ContactManager \控制器\ HomeController.cs 123 25的ContactManager

錯誤2' ContactManager.Models.Contact'會不包含'EntityKey'的定義 ,並且沒有找到'ContactManager.Models.Contact'類型的第一個 自變量的擴展方法'EntityKey'可以被發現( 您缺少使用指令或程序集 參考?)c: \用戶\ sp_admin \的文檔\ Visual Studio的 2013 \項目\的ContactManager \的ContactManager \控制器\ 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我認爲一些ing在這裏是否缺少?但是這個 是從模板生成的。

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

任何想法可能是錯誤的,因爲從模板生成該代碼? 謝謝

回答

2

我想你只需要調用Romove方法刪除聯繫人的實例。請看下圖:

try 
    { 
     _entities.Contacts.Remove(contactToDelete); 
     _entities.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

更新: 當您更新的Contact一個實例:

_entities.Contacts.Attach(updatedContact); 
_entities.Entry(updatedContact).State = EntityState.Modified; 
_entities.SaveChanges(); 

欲瞭解更多信息,看看下面的鏈接,這是使用實體框架有關CRUD操作的好文章。 CRUD Operations Using Entity Framework

+0

我得到了與編輯相同的問題,你知道什麼是適當的期限編輯? _entities,聯繫人。 ????我無法找到與更新相同的內容。謝謝 – user3127986

+0

看看下面的鏈接,這是一篇關於使用Entity Framework進行CRUD操作的好文章。 http://www.codeproject.com/Articles/640302/CRUD-Operations-Using-Entity-Framework-5-0-Code-Fi – Lin

+0

@ user3127986,我更新了關於「編輯」的答案。 – Lin

相關問題