c# - Entity Framework 插入新行而不是更新它们

标签 c# asp.net entity-framework asp.net-mvc-4

我在将数据更新到数据库时遇到问题。当我想更新数据时, Entity Framework 会向可以有多行的表(具有外键的表)添加新行。

数据库模型:

Database model

当我更新电话/联系人或标签实体时, Entity Framework 会自动添加新行而不是更新它

这是我使用的代码:

public string UpdateContact(Contact contact)
{
    if (contact != null)
    {

        int id = Convert.ToInt32(contact.id);
        Contact Updatecontact = db.Contacts.Where(a => a.id == id).FirstOrDefault();
        Updatecontact.firstname = contact.firstname;
        Updatecontact.lastname = contact.lastname;
        Updatecontact.address = contact.address;
        Updatecontact.bookmarked = contact.bookmarked;
        Updatecontact.city = contact.city;
        Updatecontact.notes = contact.notes;
        Updatecontact.Emails1 = contact.Emails1;
        Updatecontact.Phones1 = contact.Phones1;
        Updatecontact.Tags1 = contact.Tags1;
        db.SaveChanges();
        return "Contact Updated";

    }
    else
    {
        return "Invalid Record";
    }
}

编辑:

这是 EF 模型代码:

联系方式:

public partial class Contact
{
    public Contact()
    {
        this.Emails1 = new HashSet<Email>();
        this.Phones1 = new HashSet<Phone>();
        this.Tags1 = new HashSet<Tag>();
    }

    public int id { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public Nullable<byte> bookmarked { get; set; }
    public string notes { get; set; }

    public virtual ICollection<Email> Emails1 { get; set; }
    public virtual ICollection<Phone> Phones1 { get; set; }
    public virtual ICollection<Tag> Tags1 { get; set; }
}

Emails/Tags 和 Phone 具有相同的模型(值的名称不同)

public partial class Email
{
        public int id { get; set; }
        public int id_contact { get; set; }
        public string email1 { get; set; }

        public virtual Contact Contact1 { get; set; }
}

最佳答案

更新属性而不是设置新对象。

  Updatecontact.Emails1.email1 = contact.Emails1.email1;
  Updatecontact.Phones1.number = contact.Phones1.number;
  Updatecontact.Tags1.tag1 = contact.Tags1.tag1;

编辑:您的联系人模型似乎有电子邮件、电话和标签的列表。如果是这样,那么简单的分配将不起作用。相反,当从客户端发送时,您必须逐一查找并更新:

 foreach ( var email in contact.Emails1 )
 {
     // first make sure the object is retrieved from the database 
     var updateemail = Updatecontact.Emails1.FirstOrDefault( e => e.id == email.id );
     // then update its properties
     updateemail.email1 = email.email1;
 }

 // do the same for phones and tags

关于c# - Entity Framework 插入新行而不是更新它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27711151/

相关文章:

.net - 下载 .NET 3.5 的 Entity Framework

c# - 将参数传递给 Entity Framework 中的可重用表达式

c# - 根据满足的较低级别标准获取顶级实体

c# - 只有 1 个主键的 EntityFramework 链接表

c# - Lotus Notes 客户端中的日历项目正在另存为草稿

asp.net - 即使发现 nullException,仍继续运行代码

asp.net - SaveChangesAsync() 不更新数据库

c# - 仅在 Visual Studio 中访问 win 表单控件会从不同线程引发异常

c# - 为什么在 VB.NET 和 C# 中根据值检查 null 会有所不同?

javascript - 在 javascript datepicker (asp.net) 中禁用过去的日期