I have a table in SQL Server named EffectiveRole
that has three columns
[UserID] [int] NOT NULL,
[DealID] [int] NULL,
[RoleID] [int] NOT NULL
and it has an index IX_EffectiveRole
on columns
([UserID] ASC,
[DealID] ASC,
[RoleID] ASC
My POCO class for this table is
[Table("EffectiveRole")]
public partial class EffectiveRole
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserID { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int? DealID { get; set; }
[Key]
[Column(Order = 2)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int RoleID { get; set; }
}
When I try to insert an entity by this code
var context = new Ema171Entities();
var effectiveRole = new EffectiveRole { DealID = null, UserID = 85, RoleID = 14 };
context.EffectiveRoles.Add(effectiveRole);
context.SaveChanges();
I get an error:
The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: The object state cannot be changed. This exception may result from one or more of the primary key properties being set to null. Non-Added objects cannot have null primary key values
How can I solve this? Any ideas?