Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Assign value to column with reference, using entity framework

I have a problem assigning a value to an entity that has a reference. I get the intellisense and all but I get a null-reference exception when I try to assign it to the object passed into the function that saves to database.

        public ActionResult BookingViewEdit([Bind(Include = "BookingViewID,Enabled,ObjectLimit,RSSenabled")]BookingView bv, int selCustomers)
    {
        bv.Customers.CustomerID = selCustomers;

        _bvs.SaveBookingView(bv);

Whats needed to do to assign the value for CustomerID? the FK-key is in the "BookingView"-table, and if I just hit "bv." there is no CustomerID there.

Thanks in advance

/M

Upvotes: 0

Views: 477

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126587

Is "Customers" actually a single Customer, not a list?

In that case, you could do something like:

bv.CustomerReference.EntityKey = new EntityKey("MyEntities.Customers", "CustomerId", selCustomers);

Obviously, replace "MyEntities.Customers" with the actual entity context and entity set names.

I'll add that it's extremely confusing to use plural argument/property names for single objects.

Upvotes: 1

Related Questions