【问题标题】:ApplyPropertyChanges does not contains a definition using MVCApplyPropertyChanges 不包含使用 MVC 的定义
【发布时间】: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


    【解决方案1】:

    我想你只需要调用Remove 方法来删​​除Contact 的实例。见下文:

    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

    【讨论】:

    • 我在编辑时遇到了同样的问题,你知道编辑的正确术语是什么吗? _实体,联系人。 ???我找不到与更新相同的内容。谢谢
    • 看看下面的链接,这是一篇关于使用Entity Framework 操作CRUD 的好文章。 codeproject.com/Articles/640302/…
    • 现在可用于编辑,但是当我删除它时,delete.aspx 进行确认,但当我执行返回以列出已删除的 id 时,它没有删除选定的行仍然存在,任何想法 ?我执行了你回答的代码
    • 刷新页面时,被删除的行还在吗?如果它仍然存在,请检查您是否使用Seed
    猜你喜欢
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多