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.

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

thank you Sir.

God bless :)

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

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

thank you sir. God bless. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.