Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7845903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:24:04+00:00 2026-06-02T17:24:04+00:00

I am totally confused with update in EF. Tried to find related tutorial over

  • 0

I am totally confused with update in EF. Tried to find related tutorial over internet but found nothing.

I have two question in mind.

  1. How to Update only specific field of some table.
  2. How update works with navigational property?(i. if i update some entity then Ef(Linq to Entity) also update navigational property of it?)

    [Table("master")]
    public class masterViewModel
    {
      [Key,ForeignKey("address")]
      public int id { get; set; }
      public string name { get; set; }
      public string lastName { get; set; }
      public virtual address address { get; set; }
    }
    
    public class address
    {
    [Key]
    public int id { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    

    }

    i.e suppose i have [masterViewModel.id=1 , masterViewModel.name="ABC" and masterViewModel.lastName=null ] [address.id=1 , address.city="xyz" and address.state=null] then i at my view i did something like below

        @Html.DisplayFor(modelModel => Model.name)
        @Html.DisplayFor(modelModel => Model.address.city)
        @Html.HiddenFor(x => x.id)@Html.HiddenFor(x => x.address.id)
        @Html.EditorFor(modelModel => Model.address.state )
        @Html.EditorFor(modelModel => Model.lastName)
    

Because i used DisplayFor for name and string property i never gt value of these two field at my Controller action when form post, So i need something that that update only “state” (of address) and “lastName” (of masterViewModel) property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:24:06+00:00Added an answer on June 2, 2026 at 5:24 pm

    One way is to us hidden fields for other properties as @Kyle suggested but it result in transporting unneeded properties from client to server and back. It also expose additional vulnerability because malicious client can create post request which will send modified name and city as well so it will requires some additional validation logic on your server side.

    If you don’t want to update all properties you must tell it to EF. Suppose that you received your masterViewModel with related address with modified data to your controller’s action. Now you can do something like this:

    using (var context = new YourDbContext())
    {
        // This will inform EF about your existing masterViewModel and related address
        context.MasterViewModels.Attach(masterViewModel);
        // Now you must explicitly tell which properties you want to update
        var masterEntry = context.Entry(masterViewModel);
        masterEntry.Property(m => m.lastName).IsModified = true; 
        var addressEntry = context.Entry(masterViewModel.address);
        addressEntry.Property(a => a.state).IsModified = true;
        context.SaveChanges(); 
    }
    

    Btw. in your case it looks like address can be complex type stored in your master table instead of separate entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be something very simple but I'm totally confused. I have a JScrollPane
Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
Totally confused by this one... We have a WAMPServer installation set up, running a
I am totally confused between WCF and ASMX web services. I have used a
Totally new to programming so kindly excuse the silly question. I have this URL
Okay I'm totally confused on this one. I have a script that receives a
I am totally confused by mySQLi. Although I have been using procedural mysql calls
Totally confused about the data types required here. I have this Linq statement: var
I am totally confused with the following error. I have my application which works
I'm totally confused and have no idea how to do this, so please forgive

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.