I'm using the Entity Framework v4 for a small project. Normally I use NHibernate. My issue is that I accidentally had code which added an object which was already persisted to the DB Context's collection, and when I did a SaveChanges(), the EF made a copy of the object, giving it a new Primary Key.
While this is useful, it's not what I wanted. Is there a way to turn off that functionality, and instead have an exception thrown?
UPDATE: CODE NOW INCLUDED
public class CcUser
{
public int Id { get; set; }
[StringLength(50)]
public string TrackingId { get; set; }
[StringLength(50)]
public string MembershipGuid { get; set; }
public bool CookiesConfirmed { get; set; }
[StringLength(200)]
public string Email { get; set; }
public DateTime Modified { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<CcUser> Users { get; set; }
}
MyDbContext db = new MyDbContext();
var ccUser = db.Users.FirstOrDefault(u => u.TrackingId == id);
ccUser.Modified = DateTime.UtcNow;
db.Users.Add(ccUser);
db.SaveChanges();