EF4 CTP5 Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.
- by user658332
hi,
I am new to EF4 CTP5. I am just hanging at one problem..
I am using CodeFirst without Database, so when i execute application, it generated DB for me.
here is my scenario, i have following Class structure...
public class KVCalculationWish
{
public KVCalculationWish()
{
}
public int KVCalculationWishId { get; set; }
public string KVCalculationWishName { get; set; }
public int KVSingleOfferId { get; set; }
public virtual KVSingleOffer SingleOffer { get; set; }
public int KVCalculationsForPersonId { get; set; }
public virtual KVCalculationsForPerson CaculationsForPerson { get; set; }
}
public class KVSingleOffer
{
public KVSingleOffer()
{
}
public int KVSingleOfferId { get; set; }
public string KVSingleOfferName { get; set; }
public KVCalculationWish CalculationWish { get; set; }
}
public class KVCalculationsForPerson
{
public KVCalculationsForPerson()
{
}
public int KVCalculationsForPersonId { get; set; }
public string KVCalculationsForPersonName { get; set; }
public KVCalculationWish CalculationWish { get; set; }
}
public class EntiyRelation : DbContext
{
public EntiyRelation()
{
}
public DbSet<KVCalculationWish> CalculationWish { get; set; }
public DbSet<KVSingleOffer> SingleOffer { get; set; }
public DbSet<KVCalculationsForPerson> CalculationsForPerson { get; set; }
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<KVCalculationWish>().HasOptional(m => m.SingleOffer).WithRequired(p => p.CalculationWish);
modelBuilder.Entity<KVCalculationWish>().HasOptional(m => m.CaculationsForPerson).WithRequired(p => p.CalculationWish);
}
}
i want to use KVCalcuationWish object in KVCalcuationsForPerson and KVSingleOffer class.
So when i am creating object of
KVCalcuationForPerson and KVSingleOffer class i initialize both object with New KVCalcuationWish object. like this
KVCalculationsForPerson calcPerson = new KVCalculationsForPerson();
KVCalculationWish wish = new KVCalculationWish() { CaculationsForPerson = calcPerson };
calcPerson.KVCalculationsForPersonName = "Person Name";
calcPerson.CalculationWish = wish;
KVSingleOffer singleOffer = new KVSingleOffer();
KVCalculationWish wish1 = new KVCalculationWish() { SingleOffer = singleOffer };
singleOffer.KVSingleOfferName = "Offer Name";
singleOffer.CalculationWish = wish1;
but my problem is when i save this records using following code
try
{
db.CalculationsForPerson.Add(calcPerson);
db.SingleOffer.Add(singleOffer);
db.SaveChanges();
}
catch (Exception ex)
{
}
i can save successfully in DB, but in Table KVCalcuationWish i am not able to get the ID of SingleOffer and CalcuationsForPerson class object.
Following is the data of KVCalcuationWish table.
KVCalcuationWishID KVCalcuationWishName KVSingleOfferID KVCalcuationsForPersonID
1 NULL 0 0
Following is the data of KVSingleOFfer Table
KVSingleOfferID KVSingleOfferName
1 Offer Name
Follwing is the data of KVCalcuationsForPerson Table
KVSingleOfferID KVSingleOfferName
1 Person Name
I want to have following possible output in KVCalcuationWish table.
KVCalcuationWishID KVCalcuationWishName KVSingleOfferID KVCalcuationsForPersonID
1 NULL 1 NULL
2 NULL NULL 1
so what i want to achieve is ......
when i am save KVSingleOffer object i want separate record to be inserted and when i save KVCalcuationsForPerson object another separate record should be save to KVCalcuationwish table.
Is that possible?
Sorry for long description... but i really hang on this situation...
Thanks & Regards,
Joyous Suhas