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 6136787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:38:44+00:00 2026-05-23T17:38:44+00:00

I am trying to setup editing functionality using MVC3 and entity framework 4. I

  • 0

I am trying to setup editing functionality using MVC3 and entity framework 4. I have a navigation property adding to my customer entity to allow me to access a one to many related entity. I have the related entity defined as virtual – its the Customer property in the below CustomerSite entity

namespace CustomerOrders.Domain.Entities
{
public class CustomerSite
{
    [HiddenInput(DisplayValue = false)]
    [Key]
    public int CustomerSiteId { get; set; }
    [HiddenInput(DisplayValue = false)]
    [DisplayName("Customer Id")]
    public int CustomerId { get; set; }
    [Required(ErrorMessage = "Please enter a unit no/street name")]
    [DisplayName("Address Line 1")]
    public string AddressLine1 { get; set; }
    public virtual Customer Customer { get; set; }

}
}

When i try to save an edit to the CustomerSite entity i get the following exception

Object reference not set to an instance of an object.

Now when i inspect the model values, the only null value is the Customer navigation property so i assuming thats whats throwing the error? EF must automatically Populate the foreign key property as the customer id field is correctly populated when i inspect the model. Thats the field i was ecpecting to be a null as i havent done anyhting extra to insert the customer ID which i am not rendering an editor for in the view.

This is my save method

  public void SaveCustomerSite(CustomerSite customerSite)
    {
        if (customerSite.CustomerSiteId == 0)
        {
            context.CustomerSites.Add(customerSite);
        }
        context.SaveChanges();

        }

And this is how i have the relationship defined in the EF data context

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CustomerSite>()
            .HasRequired(x => x.Customer);
    }

Does anyone know if the navigation property being null at the time the edit is saved will be what is causing the excpetion and if so how can i resolve this?

All advice is appreciated.

EDIT================

This is the stack trace from the exception

at CustomerOrders.WebUI.Controllers.SiteAdminController.Edit(CustomerSite  customerSite) in C:\Users\administrator\documents\visual studio 2010\Projects \CustomerOrders\CustomerOrders.WebUI\Controllers\SiteAdminController.cs:line 43
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[]   parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext  controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15. <InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  • 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-05-23T17:38:45+00:00Added an answer on May 23, 2026 at 5:38 pm
     if (customerSite.CustomerSiteId == 0)
    

    If customerSite is null when it’s passed in, you cannot reference any properties.

    Test for null before this line.

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

Sidebar

Related Questions

I'm trying to setup VIM for editing Actionscript 3 for some upcoming Flash projects.
I'm trying setup a subset of boost and get it properly compiled using bjam,
I'm trying to setup an Apache/PHP/Postgresql server locally on my machine. I'm using Windows
I am trying to setup an edit form for my User object. I have
I have been trying to setup Ruby on Rails for the past 2 days
Trying to setup a test page that access a video hosted on S3 and
trying to setup drupal's taxonomy module ( http://drupal.org/project/taxonomy_access ) to administer access to specific
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to setup a CodeIgniter based project for local development (LAMP stack), and once
After trying to setup my site for Google Webmaster Tools I found that my

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.