asp.net mvc 3 - Entity Framework 4 Navigation property null value throws an exception -



asp.net mvc 3 - Entity Framework 4 Navigation property null value throws an exception -

i trying setup editing functionality using mvc3 , entity framework 4. have navigation property adding client entity allow me access 1 many related entity. have related entity defined virtual - client property in 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 come in unit no/street name")] [displayname("address line 1")] public string addressline1 { get; set; } public virtual client customer { get; set; } } }

when seek save edit customersite entity next exception

object reference not set instance of object.

now when inspect model values, null value client navigation property assuming thats whats throwing error? ef must automatically populate foreign key property client id field correctly populated when inspect model. thats field ecpecting null havent done anyhting insert client id not rendering editor in view.

this save method

public void savecustomersite(customersite customersite) { if (customersite.customersiteid == 0) { context.customersites.add(customersite); } context.savechanges(); }

and how have relationship defined in ef info context

protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<customersite>() .hasrequired(x => x.customer); }

does know if navigation property beingness null @ time edit saved causing excpetion , if how can resolve this?

all advice appreciated.

edit================

this stack trace 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 @ lambda_method(closure , controllerbase , object[] ) @ system.web.mvc.actionmethoddispatcher.execute(controllerbase controller, object[] parameters) @ system.web.mvc.reflectedactiondescriptor.execute(controllercontext controllercontext, idictionary`2 parameters) @ system.web.mvc.controlleractioninvoker.invokeactionmethod(controllercontext controllercontext, actiondescriptor actiondescriptor, idictionary`2 parameters) @ system.web.mvc.controlleractioninvoker.<>c__displayclass15. <invokeactionmethodwithfilters>b__12() @ system.web.mvc.controlleractioninvoker.invokeactionmethodfilter(iactionfilter filter, actionexecutingcontext precontext, func`1 continuation)

if (customersite.customersiteid == 0)

if customersite null when it's passed in, cannot reference properties.

test null before line.

asp.net-mvc-3 entity-framework-4 repository

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -