Answered by:
Entity Framework Insert with associations Problems

Question
-
Hi
I went the model first route, created my classes and generated the database.
Amongst that, 3 tables have been created (Club, League & Manager - where a club belongs to a league, and each club has a manager), with their foregin key references, all looks correct on the database side.
In my MVC web application, when inserting a new record, no matter how I assign the Manager and league, it seems that the sql insert statements written by entity framework do not include the ID's for the league and manager columns. I am constantly running into:
Cannot insert the value NULL into column 'Manager_Id', table 'KZNIHA.dbo.Clubs'; column does not allow nulls. INSERT fails. The statement has been terminated.
I've tried this:
using (var model = ModelProvider.GetModelContainer())
{
newClub.League = model.Leagues.Where(l => l.Id == leagueID).First();
newClub.Manager = model.Contacts.Where(c => c.Id == mgrId).First();model.AddToClubs(newClub);
model.SaveChanges();
}
}
And this:
using (var model = ModelProvider.GetModelContainer())
{
newClub.League = (from l in model.Leagues where l.Id == leagueID select l).First();
newClub.Manager = (from c in model.Contacts where c.Id == mgrId select c).First();model.AddToClubs(newClub);
model.SaveChanges();
}
And even this:
using (var model = ModelProvider.GetModelContainer())
{
newClub.LeagueReference.EntityKey = new System.Data.EntityKey("ModelContainer.Leagues", "Id", leagueID);
newClub.ManagerReference.EntityKey = new System.Data.EntityKey("ModelContainer.Contacts", "Id", mgrId);model.AddToClubs(newClub);
model.SaveChanges();}
Nothing works. No matter how i assign the sub class properties, the id's do not seem to make it into the sql insert statement.
Can anyone point me towards the light here? What am I doing wrong?Thursday, March 18, 2010 7:18 PM
Answers
-
Hello Resin,
Welcome to ADO.NET Entity Framework and LINQ to Entities forum!
It seems that there is no Manager object returns from this query: model.Contacts.Where(c => c.Id == mgrId).First(). However, the Manager_Id column in the Club table is non-nullable. Could you please debug the application to see whether there is certain Manager object whose Id equals mgrId. Besides, it is a little confusing that why the Manager object is included in the Contacts collection. Is there any inheritance here?
Also, due to the foreign key association in EF4, we can actually keep the foreign key column in the entity. So we can directly set the newClub.League_ID and newClub.Manager_ID values. For detail, please see
http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx
http://msdn.microsoft.com/en-us/library/ee373856%28VS.100%29.aspx
If you have any questions, please feel free to let me know.
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed as answer by Jonathan Aneja -- MSFT Friday, March 19, 2010 9:11 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Monday, March 29, 2010 3:33 AM
Friday, March 19, 2010 5:18 AM -
Hi Lingzhi
Thanks for the response. The Manager property is actually of type Contact - and yes, there is a corresponding contact for that ID in the Contacts table. It's a pity we can't add screenshots to posts as it would make everything easier to explain. The MVC form displays a contacts lookup control, and it is passing the correct ID value back.
Now this code used to work fine. I then ran into the same problem in another part of the application, and when I couldn't solve it, i deleted the database, recreated from the model, and it worked (in that part). It did however cause this section to stop working (without any code changes) - which i picked up when my unit tests started failing when running for the whole application.
I am running Visual Studio 2010 RC - and suspect that there may be a bug in this version of entity framework, as i can get past the problem by rebuilding the database script from the model, and dropping and recreating the db - but this does seem to introduce the same problem in other objects. As I can't step into the EF code though, it's almost impossible to verify whether it is a problem in EF or not.
- Proposed as answer by Jonathan Aneja -- MSFT Friday, March 19, 2010 9:11 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Monday, March 29, 2010 3:33 AM
Friday, March 19, 2010 9:37 AM
All replies
-
Hello Resin,
Welcome to ADO.NET Entity Framework and LINQ to Entities forum!
It seems that there is no Manager object returns from this query: model.Contacts.Where(c => c.Id == mgrId).First(). However, the Manager_Id column in the Club table is non-nullable. Could you please debug the application to see whether there is certain Manager object whose Id equals mgrId. Besides, it is a little confusing that why the Manager object is included in the Contacts collection. Is there any inheritance here?
Also, due to the foreign key association in EF4, we can actually keep the foreign key column in the entity. So we can directly set the newClub.League_ID and newClub.Manager_ID values. For detail, please see
http://blogs.msdn.com/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx
http://msdn.microsoft.com/en-us/library/ee373856%28VS.100%29.aspx
If you have any questions, please feel free to let me know.
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Proposed as answer by Jonathan Aneja -- MSFT Friday, March 19, 2010 9:11 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Monday, March 29, 2010 3:33 AM
Friday, March 19, 2010 5:18 AM -
Hi Lingzhi
Thanks for the response. The Manager property is actually of type Contact - and yes, there is a corresponding contact for that ID in the Contacts table. It's a pity we can't add screenshots to posts as it would make everything easier to explain. The MVC form displays a contacts lookup control, and it is passing the correct ID value back.
Now this code used to work fine. I then ran into the same problem in another part of the application, and when I couldn't solve it, i deleted the database, recreated from the model, and it worked (in that part). It did however cause this section to stop working (without any code changes) - which i picked up when my unit tests started failing when running for the whole application.
I am running Visual Studio 2010 RC - and suspect that there may be a bug in this version of entity framework, as i can get past the problem by rebuilding the database script from the model, and dropping and recreating the db - but this does seem to introduce the same problem in other objects. As I can't step into the EF code though, it's almost impossible to verify whether it is a problem in EF or not.
- Proposed as answer by Jonathan Aneja -- MSFT Friday, March 19, 2010 9:11 PM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Monday, March 29, 2010 3:33 AM
Friday, March 19, 2010 9:37 AM -
P.S. If I step through the code - after setting the Manager property, it is no longer null.
I can view it in the watch, and see all the correct data there. The sql statement to insert the Club object just does not include the Manager_ID.
Friday, March 19, 2010 9:48 AM -
OK - got it working. Again no code changes were required. Instead I deleted my model - generated it from the database, and it just worked. Waiting to find the next table this happens on.
Is this a bug in EF?
Friday, March 19, 2010 1:14 PM -
Hello Resin,
It’s a really strange problem. If it is convenient for you, could you send me a demo project to repro this issue? My mail address is v-micsun@microsoft.com. Thanks a lot!
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Saturday, March 20, 2010 5:15 AM -
Hi,
I have run into the same issue and I think I found the problem. The cause may be different to the case listed here but the outcome is the same.
In my case on the entity containing the primary key the key field had StoreGeneratedPattern changed to Identity, now in my case this entity key was not an identity column at all, once I corrected this everything works as expected.
This appears to be a bug as the model has changed a number of fields StoreGeneratedPattern values when I do a refresh.
Thanks,
Reeve.
Sunday, April 11, 2010 1:18 AM