0

for example.

name of field:

f1 = "field1", f2 = "field2", f3 = "field3", f4 = "field4"

how can I update the value of f2 and f3 without changing the value of f1 and f4.

f2 = "new field2", f2 = "new field3"

the new value of fields are.

f1 = "field1", f2 = "new field2", f3 = "new field3", f4 = "field4"

by using linq.

Edited by JerrimePatient

2
Contributors
5
Replies
16
Views
2 Years
Discussion Span
Last Post by JerrimePatient
0
  1. Get the current item from the database

  2. Update just the properties you want to change

  3. Use the Repository Pattern to execute code simillar to that below

        public void Update(Contact contact)
        {
            using (var context = new EfContext())
            {
                Contact existingContact = context.Contacts.Single(c => c.Id == contact.Id);
    
                context.Entry(existingContact).CurrentValues.SetValues(contact);
    
                context.SaveChanges();
            }
        }
    
0

sir, what is Contact in line 5?

        public void Update(Contact contact)
        {
            using (var context = new EfContext())
            {
                Contact existingContact = context.Contacts.Single(c => c.Id == contact.Id);
                context.Entry(existingContact).CurrentValues.SetValues(contact);
                context.SaveChanges();
            }
        }

Edited by JerrimePatient

0

In this case Contact is class. It represents a contact in my domain.

It would be a User, Person, Company, Book, Item etc

I have made the assumption you are using Entity Framework - is this in fact the case?

    public class Contact
    {
        public int Id { get; set;

        public string Firstname { get; set; }

        public string Surname { get; set; }

        public string Company { get; set; }

        public string Telephone { get; set; }

        public string Mobile { get; set; }
    }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.