An object of type cannot be set or removed from the Value property of an EntityReference of type

c# ef-code-first entity-framework entity-framework-6 navigation-properties

Question

I keep getting this problem when I try to add an object to a navigation property of an entity.

System.InvalidOperationException
An object of type 'System.Collections.ObjectModel.Collection`1[[WakeSocial.BusinessProcess.Core.Domain.UserDevice, WakeSocial.BusinessProcess.Core.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' cannot be set or removed from the Value property of an EntityReference of type 'WakeSocial.BusinessProcess.Core.Domain.UserDevice'.

Entity addition

    UserDevice device = new UserDevice();
    device.DeviceType = type;
    device.DeviceId = deviceId;
    device.PushId = pushId;
    device.HasFrontFacingCamera = hasFrontCamera;

    user.Devices.Add(device);
    await UpdateUser(user);


  public async Task<User> UpdateUser(User user)
  {
      Context.Entry(user).State = EntityState.Modified;
      await uow.SaveChangesAsync();
      return user;
  }

Domain model I use

public enum Gender
{
    Male = 0,
    Female = 1
}
public class User
{
    public Guid UserId { get; private set; }

    public string FacebookUserId { get; set; }
    public string FacebookAccessToken { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Gender Gender { get; set; }
    public int TokenCount { get; set; }


    public virtual ICollection<UserDevice> Devices { get; set; }
    public virtual ICollection<Beat> OwnedBeats { get; set; }

    public User()
    {
        UserId = GuidComb.Generate();
        Devices = new Collection<UserDevice>();
    }
}

I will encounter a null reference exception if I choose to delete the collection initialization and then try to add the object.

System.NullReferenceException
Object reference not set to an instance of an object.

So how are navigation properties initialized is my query. I mistakenly believed that the virtual keyword would take care of everything.

UPDATE

The DbContext includes this.

      //User
      modelBuilder.Entity<User>()
          .HasOptional(u => u.Devices)
          .WithMany()
          .WillCascadeOnDelete();

UserDevice

 public class UserDevice
{
    public Guid UserDeviceId {get;private set;}
    public DeviceType DeviceType { get; set; }
    public string DeviceId { get; set; }
    //local GSM/Apple Push Id
    public string PushId { get; set; }
    //RegistrationId from Azure Hubs
    public string HubId { get; set; }

    public bool HasFrontFacingCamera { get; set; }

    public UserDevice()
    {
        UserDeviceId = GuidComb.Generate();
    }
}
1
1
8/24/2015 8:52:41 PM

Popular Answer

Do this:

modelBuilder.Entity<User>()
          .HasMany(u => u.Devices)
          .WithOptional(d => d.User) 
          .Map(c => c.MapKey("UserId"));
0
8/24/2015 6:21:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow