I’m new to EF and have read some Tutorials and Googled many days to find a solution for my problem. Sorry my english is not the best but i want try to formulate my Question.
Data Model
My Model created from SQL Express Database (shrinked Version to better Understand) :
Create Sample Data
Now i try to fill my DB with Demo Data. I create an Object of Liegenschaft and fill data in this. Simple Version to show my Tree of Data (real Version can have many Object of HAUS in ObjektMenge=List and for ObjektMenge=List too.
List<Liegenschaft> LL = new List<Liegenschaft>();
for (int i = 0; i < 2; i++)
{
Liegenschaft L = new Liegenschaft()
{
Strasse = "demostreet1",
HausMenge = new List<Haus>(){
new Haus(){
Nummer = 21,
ObjektMenge = new List<Objekt>(){
new Objekt(){
Zimmer = 221,
MieterMenge= new List<Mieter>(){
new Mieter(){
Name="DemoName",
MietzinsMenge= new List<Mietzins>(){
new Mietzins(){
StartDate=System.DateTime.Now,
EndDate=System.DateTime.Now},
new Mietzins(){
StartDate=System.DateTime.Now,
EndDate=System.DateTime.Now}
}
}
}
}
}
}
}
};
LL.Add(L);
}
Try to save to DB
foreach (Liegenschaft Lieg in LL)
{
using (var db = new ImmoReportsEntities())
{
db.Liegenschaft.Add(Lieg);
db.SaveChanges();
}
}
Problem
All of my Demo Data can be saved to Database and all FK’s are automatically correct created when the MietzinsMenge are NOT Filled!
If i have Data in This MietzinsMenge gives an error: FK_Mietzins_Objekt has identical Primary Key!
Info
I don’t use Forms to insert Data to this Tabel’s all Data come from another (external) DB and i want to import this to my DB. How can i import this MietZins too in my Data Tree and have all of this FK (LiegenschftId, ObjektId, MieterId).
It doesn’t look like you are setting primary key values for your Mietzins objects. You need to either set the primary key values before trying to save to the database or tell EF that the primary keys will be generated by the database.
How you specify that the key is generated by the database depends on whether you are using Code First or Database First. In Code First, it’s usualy on by default for integer key types, or you can use the DatabaseGeneratedAttribute and set Identity. For Database First, you use the EF Designer and set the StoreGenerated facet of the key property to Identity.
I re-created your model as shown in the diagram using the following classes and Code First mappings. (I recognize that you are probably not using Code First, but this was the easiest way for me to create the same model with all the relationships as shown in the diagram.)
Context:
I then ran your code exactly as in the question and it worked fine.
I then changed the primary key of Mietzins so that it is no longer database generated:
I ran the code again and got the following exception:
Unhandled Exception: System.Data.Entity.Infrastructure.DbUpdateException:
An error occurred while updating the entries.
See the inner exception for details.
—>
System.Data.UpdateException: An error occurred while updating the entries.
See the inner exception for details.
—>
System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint ‘PK_Mietzins’. Cannot insert duplicate key in object ‘dbo.Mietzins’.
The statement has been terminated.
If this is different than the exception you are seeing then please post your code and the full exception message and stack trace and I’ll take another look.