I have started with C# and I wanted to make my own DB.
I have two models
public class AModel
{
public Guid ID { get; private set; }
public string Name { get; set; }
public int Count { get; set; }
public AModel()
{
this.ID = Guid.NewGuid();
}
}
public class BModel
{
public Guid ID { get; private set; }
public string Name { get; set; }
public AModel Model { get; set; }
public BModel()
{
this.ID = Guid.NewGuid();
}
}
When I try to save BModel to DB, I get this error:
Violation of PRIMARY KEY constraint 'PK_dbo.AModels'. Cannot insert duplicate key in object 'dbo.AModels'. The duplicate key value is (48ee1711-8da4-46c1-a714-19e985211fed). The statement has been terminated.
I thought it would be solved by this
modelBuilder.Entity<BModel>().HasRequired(t => t.Model).WithMany();
but it looks like I am completely lost. Could anybody help me with this simple example?
See Question&Answers more detail:os