views:

64

answers:

2

Hello

I have a problem adding entities to a collection.

public void SaveNotificationUsergroups(int bookingobjectID, int[] NotificationUsergroups)
    {

        BookingObject bo = _entities.BookingObjectSet.Where(b => b.BookingObjectID == bookingobjectID).First();

        bo.UsergroupsBookingNotifications.Load();

        int _currentUsergroupID;

        for (int i = 0; i < NotificationUsergroups.Count(); i++)
        {
            _currentUsergroupID = NotificationUsergroups[i];

            bo.UsergroupsBookingNotifications.Add(_entities.UsergroupSet.Where(ug => ug.UsergroupID == _currentUsergroupID).First();
        }

        _entities.SaveChanges();

    }

I get an error on:

bo.UsergroupsBookingNotifications.Add(_entities.UsergroupSet.Where(ug => ug.UsergroupID == _currentUsergroupID).First();

that is:

cannot convert from 'BookingSystem.Data.Models.Usergroup' to 'BookingSystem.Data.Models.UsergroupsBookingNotifications'

In the database UsergroupsBookingNotifications has "int BookingObjectID" and "int UsergroupID"

whats needed to do?

/M

EDIT:

Tables:

Usergroups <---> UsergroupsBookingNotifications <--> Bookings

A: 

It seems to me that you can't add a UserGroup into a UsergroupsBookingNofication. I don't know for sure because I don't know what your relationships are.

Dave Arkell
+2  A: 

The reason that you are getting this error is because bo.UsergroupsBookingNotifications is a collection of UsergroupsBookingNotifications and you are trying to add a Usergroup object to it.

This:

_entities.UsergroupSet.Where(ug => ug.UsergroupID == _currentUsergroupID).First();

will return a Usergroup object.

With out seeing the rest of you object model it's a bit hard to help but this might be what you are looking for:

for (int i = 0; i < NotificationUsergroups.Count(); i++)
{
    _currentUsergroupID = NotificationUsergroups[i];

    // Make a new UsergroupsBookingNotifications object
    UsergroupsBookingNotifications notify = new UsergroupsBookingNotifications();

    // Add the bookobject and usergroup
    notify.BookingObject = bo;
    notify.Usergroup = _entities.UsergroupSet.Where(ug => ug.UsergroupID == _currentUsergroupID).First();

    // Add the collection.
    bo.UsergroupsBookingNotifications.Add(notify);
}

Like I said it's a bit hard to help without some more info, but that might get you started.

Nathan W