Single entity "mapped to the same rows" - entity-framework

I saw this error:
System.Data.EntityCommandCompilationException: An error occurred while
preparing the command definition. See the inner exception for details.
---> System.Data.MappingException: SQL.Data.msl(945,10) : error 3032: Problem in mapping fragments starting at lines 945,
965:EntityTypes DIDataModel.TitleEstate are being mapped to the same
rows in table TitleEstate. Mapping conditions can be used to
distinguish the rows that these types are mapped to.
The grammar seemed odd, using "EntityTypes" plural, and "are" after a one-item list.
How to fix this?

It started working after a clean-and-rebuild, but that was only a temporary fix.
The permanent solution was (as is often the case with Entity Framework problems) to delete the entity from the .edmx diagram and add it back in again.

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.

Can't create migration to add new column to table: Invalid column name

I'm trying to add a column to an existing table. It is simply a string column, not involved in any kind of keys and this is the only change I'm trying to make. I am creating the migration with the powershell call
dotnet ef migrations add [migration name] --context [context name]
As I've done many times before. I get an error that says.
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: Invalid column name '[column id]'.
Unable to create an object of type 'DashboardContext'. Add an implementation of 'IDesignTimeDbContextFactory<[context name]>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
Apparently this error means that the column doesn't exist, but that's precisely why I'm trying to create the migration - to add it.
EDIT: It turns out that adding the migration includes a call to Startup->Configure which in my case accessed that table. Any info on why that is the case would be appreciated. Meanwhile I got around it by commenting that code out while creating the migration.

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.

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.

Entity framework - "Problem in mapping fragments"-error. Help me understand the explanations of this error

Error 3007: Problem in Mapping Fragments starting at lines 186, 205: Non-Primary-Key column(s) [WheelID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.
I found several places on the web describing this error, but I simply don't understand them. (confused smiley goes here)
One
Two
Three
Four
There is something pretty fundamental here, I must be missing. Can you explain it, so that I understand it? Maybe using my real life example below?
Foreign key 1:N Wheels.Id -> Slices.WheelId
I add them to entity framework, and WheelId is not visible in the Slices-entity.
Doing some workaround (deleting the relationship from the db before adding tables to EF - then re-creating it and updating EF) I managed to get the WheelId to stay in Slices, but then I get the error mentioned at the top.
Since Slices.WheelId is an FK, you cannot expose it in your client model, period. There are ways to get the value, though.
var wheelId = someSlice.Wheels.ID;
Update In EF 4 you can do this by using FK Associations instead of independent associations.
Try to remove foreign property column from Entity set using entity model design it will solve your problem
For example
We have two tables one is customer and other one is order, using entity model design we added association between customers and orders when we do this Ado.net entity framework i will add navigation properties to both below tables.
Like
Customer.Orders - Here order is list
Order.Customer
One - Many relation.
So we need to remove property from with name CustomerId[Foreign key column] from Order entity set.
For reference:
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2823634f-9dd1-4547-93b5-17bb8a882ac2/
I was able to overcome this problem by the following steps:
right click the designer window
Select 'update model from database'
Select Add AND make sure that the 'Include foreign key columns in the model' checkbox is selected.
click on Finish...
I had set foreign keys up in the database but framework still wasn't pulling them in correctly. So I tried to add the association myself. 
However, when I did this I would get a mapping error. It took me A WHILE but I figured out. What I did was set up the association using the entity toolbox association tool and then you have to double click on the association (1 to many) line and set the primary and foreign key there. Hopefully, this to help others who might have the same problem. I couldn't find the answer anywhere.
I had this problem for quite a different reason, and the message was slightly different; it didn't say "data inconsistency is possible because the corresponding conceptual side properties can be independently modified."
I have a table involved in my model with a binary column where I store image data. I only want this data returned when I need it (performance is a feature), so I split the table using a method similar to this. Later on, I added a property to that table, then updated the model from the database. The wizard added the property to both entity types that refer to the table with the added property. I had to delete it from one of them to solve the error.
I've had this happen because Entity Framework Update wizard mismapped some keys (or did not update?). As a result, some columns were mistakenly labeled as keys, while actual key columns were treated as plain columns.
The solution was to manually open EDMX file, find the entities, and update the keys.
Couldn't get any of the answer to work with EF6. The problem seems to be the framework doesn't import the foreign keys correctly as Associations. My solution was removing foreign keys from the tables, and then manually adding the associations using Entity Framework model, using the following steps: Entity Framework - Add Navigation Property Manually
For LinQ to Entities queries in EF1, my workaround for not having access to the foreign key as a property is with the following code, which does not produce a join query to the associated table:
dbContext.Table1s.FirstOrDefault(c => (int?)c.Table2.Id == null)
i.e, the generated SQL is:
...WHERE ([Extent1].[Table2Id] IS NULL)...
Solution is to allow deleting Rule = Cascade on Sql association.
Same thing as to be done on .edmx model, adding element to
association:
<Association Name="FK_Wheels_Slices">
<End Role="Wheels" Type= "your tipe here" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
</Association>
I had a table already mapped in EF. I added two more tables which had foreign keys in the previously added table. I then got the 3007 error.
To fix the error I deleted all three tables from the EDMX file, and then re-added them all at once together (via "Update Model from Database..."), instead of in stages.
I checked my Error List window and noticed I had errors in the model. Fixed them and all is well
in my case I solved this error by tick (include foreign key columns in the model)
- update Model from database
- tick (include foreign key columns in the model)
- finish