I am trying to save below object to database with Entity Framework code-first, database was created properly but when SaveChanges(); gets called it thows below exception.
Saving or accepting changes failed because more than one entity of type 'ConditionalOptions' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.
Can anyone take me to right direction?
I have this JSON:
{
"components": [
{
"label": "Text Field",
"key": "1",
"conditional": {
"show": "",
"when": "",
"json": ""
}
},
{
"label": "Columns",
"columns": [
{
"ParentComponentKey":"4",
"components": [
{
"label": "Text Field",
"key": "5",
"conditional": {
"show": "",
"when": "",
"json": ""
}
}
],
"Id":"1"
}
],
"key": "4",
"conditional": {
"show": "",
"when": "",
"json": ""
}
},
{
"type": "button",
"label": "Submit",
"key": "7",
"disableOnInvalid": true,
"theme": "primary",
"input": true,
"tableView": true
}
]
}
I have created these model classes to map data into an object:
public class Form
{
public Form()
{
this.Components = new HashSet<ComponentOptions>();
}
[Key]
public int Id { get; set; }
public string title { get; set; }
public virtual ICollection<ComponentOptions> Components { get; set;}
}
public class ComponentOptions
{
[Key, Column(Order = 0)]
public int FormId { get; set; }
[Key, Column(Order = 1)]
public int Key { get; set; }
public string label { get; set; }
public virtual ConditionalOptions conditional { get; set; }
public virtual ICollection<ColumnOptions> columns { get; set; }
[ForeignKey("FormId")]
public virtual Form Form { get; set; }
}
public class ConditionalOptions
{
[Key, ForeignKey("ComponentOptions"), Column(Order = 0)]
public Int64 FormId { get; set; }
[Key, ForeignKey("ComponentOptions"), Column(Order = 1)]
public int Key { get; set; }
public string show { get; set; }
public string when { get; set; }
public string eq { get; set; }
public virtual ComponentOptions ComponentOptions { get; set; }
}
public class ColumnOptions
{
//[ForeignKey("FormId,Key")]
//public virtual ComponentOptions ComponentOption { get; set; }
public ColumnOptions()
{
this.components = new HashSet<ComponentOptions>();
}
[Key, Column(Order = 0)]
public Int64 FormId { get; set; }
[Key, Column(Order = 1)]
public int ParentComponentKey { get; set; }
[Key, Column(Order = 2)]
public int Id { get; set; }
public string Key { get; set; }
public int width { get; set; }
public int offset { get; set; }
public int push { get; set; }
public int pull { get; set; }
public virtual ICollection<ComponentOptions> components { get; set; }
[ForeignKey("FormId")]
public virtual Form Form { get; set; }
}
public class EntitesContext : DbContext
{
public EntitesContext() : base("name=FBEntities")
{
Database.SetInitializer<EntitesContext>(new System.Data.Entity.CreateDatabaseIfNotExists<EntitesContext>());
}
public IDbSet<Form> FormDataSet { get; set; }
public IDbSet<ColumnOptions> ColumnOptionsDataSet { get; set; }
public IDbSet<ComponentOptions> ComponentOptionsDataSet { get; set; }
public IDbSet<ConditionalOptions> ConditionalOptionsDataSet { get; set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Entity<ComponentOptions>()
.HasMany(s => s.columns)
.WithMany(c => c.components)
.Map(cs =>
{
cs.MapLeftKey("ComponentFormId", "ComponentKey");
cs.MapRightKey("ColumnFormId", "ColumnComponentKey", "ColumnId");
cs.ToTable("ComponentColumn");
});
}
}