I'm trying to save an object to my database, but I cannot because of the above specified exception message.
Here's what I'm doing:
Player player = playerRepository.Find(universeId, playerId);
UserProperties userProperties = userPropertiesRepository.Find(User.Identity.GetUserId());
rated = new Ratings
{
Player = player,
RatingType = ratingsIdentifier,
UserProperties = userProperties
};
ratingsRepository.InsertOrUpdate(rated);
The InsertOrUpdate()
(and Save()
) method:
public void InsertOrUpdate(Ratings ratings)
{
if (ratings.RatingsId == default(int))
{
// New entity
context.Ratings.Add(ratings); // this is where the exception happens
}
else
{
// Existing entity
context.Entry(ratings).State = EntityState.Modified;
}
Save();
}
public void Save()
{
context.SaveChanges();
}
I have a basic understanding of the issue, I just don't know how to resolve it. I can tell that it has to do with the Player
and UserProperties
objects, because if they're null, it doesn't crap itself when I try to save.
My context class:
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Player> Players { get; set; }
public DbSet<ServerData> ServerDatas { get; set; }
public DbSet<Ratings> Ratings { get; set; }
public DbSet<UserProperties> UserProperties { get; set; }
// some more DbSet<>
}
I have tried reading the other topics about this, but I can't, for example, detach()
on IdentityDbContext
. I'm totally at a loss here.