Page 1 of 1

[6.80.341] Issue with Code First and insert statements

Posted: Mon 16 Apr 2012 10:10
by Gizmo
Hi!

I've been experiencing very annoing issue.
I've many-to-many relation between my two entities (both of them have abstract base classes) and the relation is mapped in both configurations for entities.

The main entity (MedicalService) config:

Code: Select all

HasMany(t => t.Icd9Components).WithMany(m => m.MedicalServices).Map(m =>
            {
                m.MapLeftKey("SERVICE_ID");
                m.MapRightKey("PROCEDURE_ID");
            });
The child entity (Icd9Extension) config:

Code: Select all

HasMany(t => t.MedicalServices).WithMany(t => t.Icd9Components).Map(m =>
            {
                m.MapLeftKey("PROCEDURE_ID");
                m.MapRightKey("SERVICE_ID");
            });
My problem is that every time I create new MedicalService entity with existing in context child (existing Icd9Extension entity) I'm getting an exception. With dbMonitor help I managed to establish that first executing insert statement is the insert into many-to-many linking table so I've got missing reference exception because there is no parent element. I suppose that the second insert would try to insert the parent element.

Code: Select all

using (var context = new DomainContext())
            {
                var data = context.Icd9Extensions.ToList();

                var newData = new MedicalServiceEntity
                    {
                        
                        Icd9Components = new List()
                    };
                newData.Icd9Components.Add(data[0]);
                context.MedicalServices.Add(newData);
                context.SaveChanges();
            }
Could You please help me with this issue ?

I've been using
6.8.341 oracle dotConnect
Entity Framework 4.1[/code]

Posted: Wed 18 Apr 2012 14:15
by Shalex
Please send us a small test project with the corresponding DDL/DML script (if necessary) to reproduce the issue in our environment.

Posted: Mon 23 Apr 2012 12:49
by Gizmo
I've send to You test project.

Re: [6.80.341] Issue with Code First and insert statements

Posted: Mon 30 Apr 2012 08:37
by Shalex
The reason of the problem is a usage of an incorrect mapping so that Entity Framework doesn't know which one entity is main/child. That's why it generates insert statements randomly without taking into account their real relationship. You can make sure in this by calling the CreateDatabaseScript() method (cast your DbContext object to IObjectContextAdapter to make this method be available), then check the SQL that is generated by Entity Framework basing on the existing mapping - there are no foreing keys.

Re: [6.80.341] Issue with Code First and insert statements

Posted: Mon 07 May 2012 09:08
by Gizmo
I don't think it's the mapping problem only. I think it mostly depend on TPC approach.
I've made a simple experiment and remove all base abstract classes, copied their properties into concrete classes.
After adjusting mapping configuration classes (moved configuration from base class mapping to concrete class mapping) the CreateDatabaseScript() method now generate the foreign keys although from the database point of view nothing should really changed.

The conclusion is - why is it working with simple classes (no inheritance) and not with TPC approach? :(

Re: [6.80.341] Issue with Code First and insert statements

Posted: Mon 07 May 2012 16:28
by Shalex
It seems like that you have encountered the limitation of TPC mapping. We do recommend you to report about this issue to Microsoft because only they can change the logic of mapping processing. There should be a message like "this situation is not supported".