1
votes

I got this error when I trying to modify the Delete method in an MVC application.

Error 1 'ContactManager.Models.ContactManagerDBEntities2' does not contain a definition for 'ApplyPropertyChanges' and no extension method 'ApplyPropertyChanges' accepting a first argument of type 'ContactManager.Models.ContactManagerDBEntities2' could be found (are you missing a using directive or an assembly reference?) c:\users\sp_admin\documents\visual studio 2013\Projects\ContactManager\ContactManager\Controllers\HomeController.cs 123 25 ContactManager

Error 2 'ContactManager.Models.Contact' does not contain a definition for 'EntityKey' and no extension method 'EntityKey' accepting a first argument of type 'ContactManager.Models.Contact' could be found (are you missing a using directive or an assembly reference?) c:\users\sp_admin\documents\visual studio 2013\Projects\ContactManager\ContactManager\Controllers\HomeController.cs 123 62 ContactManager

Here is the 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();
        }
    }

Here is the ContactManagerModel.Context.cs I think something is missing in this ?? but this was generated from a template.

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

Any idea what can be wrong since This code was generated from a template? Thanks

1

1 Answers

3
votes

I think you just need to call the Remove method to delete the instance of Contact. See below:

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

Update: When you update a instance of Contact:

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

For more information, take a look at the following link, it's a good article about CRUD operations using Entity Framework. CRUD Operations Using Entity Framework