1

I am getting error in adding the value to IEnumerable type entity. Here is the restaurant entity

public virtual ICollection<BusinessUser> BusinessUsers { get; set; }

Here is the businessUser entity

public virtual ICollection<Restaurant> Restaurants { get; set; }

Here is the action method to add business user to the restaurant

public ActionResult Add(Restaurant restaurant)
{
    if (ModelState.IsValid)
    {    
        restaurant.DateAdded = DateTime.Now;
        restaurant.BusinessUsers.Select(c => c.PersonID) = 2; // i want to assign the user with id 2 to this restaurant
        db.Restaurants.Add(restaurant);
        db.SaveChanges();
        TempData["Success"] = "New Restaurant has been added successfully";
        return RedirectToAction("Index");
    }
    return View(restaurant);
}

I see the following error in my editor:

Only assignment call increment decrement await and new object expressions can be used as a statement

Usama
  • 63
  • 9

2 Answers2

2

You must create a BusinessUser entity with PersonID = 2 and attach it to the context (to avoid that it gets duplicated in the database). Then add this entity to the restaurant.BusinessUsers collection:

public ActionResult Add(Restaurant restaurant)
{
    if (ModelState.IsValid)
    {    
        var businessUser = new BusinessUser { PersonID = 2 };
        db.BusinessUsers.Attach(businessUser);

        restaurant.DateAdded = DateTime.Now;
        restaurant.BusinessUsers.Add(businessUser);

        db.Restaurants.Add(restaurant);
        db.SaveChanges();

        TempData["Success"] = "New Restaurant has been added successfully";
        return RedirectToAction("Index");
    }
    return View(restaurant);
}

I assume that restaurant.BusinessUsers is instantiated, otherwise add the line restaurant.BusinessUsers = new List<BusinessUser>(); before adding the user to the collection.

Slauma
  • 175,098
  • 59
  • 401
  • 420
  • i want to assign restaurant business user as the current user logged in, i have got the userID. your code is adding the business user but i want to assign the current business user logged in to the restaurant. – Usama Jan 04 '14 at 12:37
  • @UsamaSheikh: The code does this exactly. It doesn't add an existing user to the database, it only creates a relationship between restaurant and your logged in (and already existing) user. – Slauma Jan 04 '14 at 12:42
  • it is creating the new entity "businessUser" – Usama Jan 04 '14 at 14:32
  • @UsamaSheikh: Does the `restaurant.BusinessUsers` collection already contain a `BusinessUser` entity when you enter the `Add` action? If yes, try to add `restaurant.BusinessUsers.Clear();` before `restaurant.BusinessUsers.Add(businessUser);`. – Slauma Jan 04 '14 at 14:47
  • still the same error, it is giving error on the actual BusinessUser entity properties with Required Attribute – Usama Jan 04 '14 at 15:00
  • @UsamaSheikh: Do you mean that `ModelState.IsValid` returns `false`? – Slauma Jan 04 '14 at 15:11
  • no it is giving "Value cannot be null. Parameter name: password" from the entity BusinessUsers – Usama Jan 04 '14 at 15:17
  • @UsamaSheikh: At which line of the code do you get this exception? – Slauma Jan 04 '14 at 15:22
  • at this line db.BusinessUsers.Attach(businessUser); – Usama Jan 04 '14 at 15:33
  • still the same error, it is checking all properties of BusinessUser entity, some of properties of that entity is with Required Attribute and it is giving error only with that properties – Usama Jan 04 '14 at 15:33
  • at this line db.BusinessUsers.Attach(businessUser); – Usama Jan 04 '14 at 15:34
  • @UsamaSheikh: OK, then try to load the user from the DB instead of attaching a stub, i.e. replace the two lines `var businessUser = new BusinessUser { PersonID = 2 }; db.BusinessUsers.Attach(businessUser);` with the line: `var businessUser = db.BusinessUsers.Find(2);`. Then all required props should be filled. – Slauma Jan 04 '14 at 15:38
  • now it is giving error "Object reference not set to an instance of an object." at line "restaurant.BusinessUsers.Add(businessUser);" right after the replaced lines – Usama Jan 04 '14 at 15:43
  • @UsamaSheikh: Add `restaurant.BusinessUsers = new List();` before that line - as said at the bottom of my answer. – Slauma Jan 04 '14 at 15:46
  • Thanks alot for your time, it worked perfectly, can u please tellme my mistake?? i shall be very gratefull – Usama Jan 04 '14 at 15:53
  • @UsamaSheikh: Your initial error `Select(...) = 2` is just illegal C#. `Select(...)` always returns a collection if type `IEnumerable` and you cannot assign a value of `T` to such a collection. The last error "Object reference not set..." (`NullReferenceException`) is pretty common and happens if you try to access any object that is `null`. You must ensure that it is *not* `null` before you access it. More details are here: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Slauma Jan 04 '14 at 16:03
  • Thanks again, can u please tellme the best professional learning resources for entity framework, linq and MVC 4? please – Usama Jan 04 '14 at 16:12
  • @UsamaSheikh: http://www.pluralsight.com/ It's not free but worth the money, especially for beginners. They offer a free trial for a few days if you like to test it first. They have a lot of courses about all 3 topics, for example: MVC4: http://www.pluralsight.com/training/Courses/TableOfContents/mvc4-building, LINQ: http://www.pluralsight.com/training/Courses/TableOfContents/linq-fundamentals, EF: http://www.pluralsight.com/training/Courses/TableOfContents/entity-framework5-getting-started – Slauma Jan 04 '14 at 16:32
0

I suspect your syntax error is coming from this line:

restaurant.BusinessUsers.Select(c => c.PersonID) == 2; 

Which should probably be something like:

restaurant.BusinessUsers.Add(db.Persons.Where(c => c.PersonID == 2).First());
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • i don't want to compare the values. i want to set the value – Usama Jan 04 '14 at 12:26
  • That is exactly what this code is doing. It selects the user with Id 2 and assigns it to `restaurant.BusinessUsers`. try it. – jessehouwing Jan 04 '14 at 12:27
  • you are right but i want to select the user i just want to set the restaurant business user to the business user with the id 2 – Usama Jan 04 '14 at 12:31
  • Then use the approach @Slauma proposes, attach the object with the right ID to the context and assign the object. – jessehouwing Jan 04 '14 at 12:32
  • i want to assign restaurant business user as the current user logged in, i have got the userID. Slauma code is adding the business user but i want to assign the current business user logged in to the restaurant. – Usama Jan 04 '14 at 12:38