How to solve ientitychangetracker? - entity-framework

var
studentClassNumber=DContext.Instance().StudentEntities.tblStudentInformation.ToList();
DContext.Instance().StudEntities.tblStudentInformation.AddRange(studentClassNumber);
DContext.Instance().StudEntities.SaveChanges();
**
Add tables to the database from the database.But I get an error in the many to many relations tables.This error is:
an entity object cannot be referenced by multiple instances of ientitychangetracker.
and I take error row
DContext.Instance().StudEntities.tblStudentInformation.AddRange(studentClassNumber);

Related

How can I map one custom Entity to some database tables in Entity Framework?

I have a database first model in my project. The 3 tables Document, DocumentItem and Product are imported from the database.
I want to create a new Entity named Order that joins some fields of these three database tables. I created that Entity as you see in the first picture and filled its table mapping as you see in the second picture.
After the build of the project I get the following errors:
Severity Code Description Project File Line Suppression State
Error Error 3025: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Product.Id) of table Product. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3025: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Document.Id) of table Document. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
Error Error 3024: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3024: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
I could not find the source of the problem. How can I solve these errors?
The error tells there's a foreign key missing in your project !
Considering that you know You cannot simply add tables from the database into your model and then create a new association in the model. By default it uses independent association which must be mapped to its database counterpart(e.g the relation must exists in the database as well). You must model your relation as FK association but it allows only one-to-one and one-to-many associations. In another words, when you add a new table with "Include foreign key columns in the model" checked in EF4 and the table doesn't contain any foreign key relationships then you try to add a association it will trigger this error. The solution is to Define Constraints in an EF4 model that don’t exist in the database.
Sometimes we have unwanted conflicts which can cause such this problems when you can delete the table from the EF designer and also delete everything related to that table manually (in the XML file), then add the table again and put the right mappings again.

EF 6 Remove entity connected to read only view

I have two entities: one based on normal DB table and another base on DB View. They are connected one to many, like this:
Entity City do not have store procedures mapped because for entity Country it is enough to have read only collection of Cities.
And cascade rule is not set.
But then I want to delete Country instance which have some Cities loaded I've received the following error:
Unable to update the EntitySet because it has a
DefiningQuery and no 'DeleteFunction' element exists in the
'ModificationFunctionMapping' element to support the current
operation.
Which forcing me to create dummy stored procedure on DB and use it as Delete function, but it is an ugly solution.
Is there any better solution?

Why do I get "unable to generate the model" errors when adding tables in EF?

Working in a team environment, someone just created some tables I needed to add (EF Database First design).
I chose to "Update model from database...", selected the new tables, and got an obscure error message:
Unable to generate the model because of the following exception: 'The value for column 'DataType' in table 'TableDetails' is DBNull.
Unable to cast object of type 'System.DBNull' to type 'System.String'.
'.
This is actually caused by trying to add a table with no primary key. Just update the tables to have a primary key and you shouldn't get this error anymore.
It would be nice if the error made this clear. It would also be nice if everyone remembered to set primary keys on tables when they created them.
Hopefully this will save others the fruitless efforts I had of searching for an answer.

Calling DB2 stored procedure from Entity Framework

I am trying to call DB2 Stored procedure from entity framework. I have created Store schema and conceptual schema based on the SP. I have specified an Entity as a return type to get the data back from SP call. At design time, i am getting the below message as warning
Warning 1 Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer DB2Container12345.
At runtime I am getting the below error..
Runtime Error :
Schema specified is not valid. Errors:
Data.DB2.msl(6,6) : error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer DB2Container12345.
Any help would be appreciated. I am almost stuck up with this point...
Thanks,
Ganesan Subbian

Entity Framework - Association From Derived Entities

I'm using the TPH (Table per Hierarchy) technique to map a set of entities.
DB Schema:
UserGroupLabelSpreads table having a "UserId", "GroupId" and "LabelId" nullable fields with some additional common fields.
DAL Objects:
- UserGroupLabelSpread abstract class.
- UserSpread with a discriminator having only non-null UserId.
- GroupSpread with a discriminator having only non-null GroupId.
- LabelSpread with a discriminator having only non-null LabelId.
I've managed to get this thing to work, but when I try to connect the UserSpread entity to an existing "User" entity, I'm getting the following error:
Error 1 Error 3034: Problem in Mapping Fragments starting at lines 487, 554: Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with overlapping keys to the same group of rows.
I've digged around to understand that the problem is that I'm mapping the UserId column twice: once for the discriminator condition and second for the association.
Am I right with my assumption? -Can I get this thing to work?
Thanks,
Nir.
There is an updated version of EDM Generator which should be able to help you. You can use it to generate, validate and more. Sorry, got the wrong link. Here is the one to v2. I believe I've had this issue. If I am not mistaken it was due to me mapping the forreign keys wrong. I was however using beta 1 of EF4 at that time and some of the messages was wrong due to the proxies. Check your forreign keys. Blog.Id ---> Blog_id was my issue. I had Blog.Id --> Blog.Id and then BlogEntry.Id ----> Blog.Blog_Id which of course doesn't work but the designer is kind of unforgiving when it comes to mapping keys.