What am I doing wrong to get "Two Entities with different keys are mapped to the same row - entity-framework

See images:
EF Designer
SQL Tables
"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."
In fact, only one of the two (or 6 here) entities will have the key for the single row.
How may I overcome this? SQL View that combines them all to one row? Calculated discriminator column in ContactMethod? How should I have designed the tables differently to work better with EF?
Or is there some XML editing I can do to keep my schema and just tell EF to trust me that I'd never put the same Id in more than one derrived class?
Here's: edmx if it helps.

The link to edmx file is not work. Perhaps, you have to add a condition tag
<Condition ColumnName="ContactId" IsNull="false" />
to your edmx file using xml-editor:
<AssociationSetMapping Name="FK_Contact_ContactMethod" TypeName="SomeNamespace.FK_Contact_ContactMethod" StoreEntitySet="ContactMethod">
<EndProperty Name="ContactMethod">
<ScalarProperty Name="ContactMethodId" ColumnName="ContactMethodId" />
</EndProperty>
<EndProperty Name="Contact">
<ScalarProperty Name="ContactId" ColumnName="ContactId" />
</EndProperty>
<Condition ColumnName="ContactId" IsNull="false" />
</AssociationSetMapping>

Taking off the foreign keys to ContactMethod in the database got rid of the errors, but doesn't seem right.

Related

Is there an equivalent to updateSchema="false" that applies to individual entities?

Setting updateSchema="false" in the configuration section of the XML file for the SQL producer will prevent Codefluent from altering the schema (i.e. field names, field type, indexes, etc) of the SQL table while still allowing for the creating of stored procedures and methods against the table. This is discussed in the blog article https://blog.codefluententities.com/2011/10/31/interoperate-with-an-existing-database-using-codefluent-entities/
Question
Is there anything built in that would achieve the same effect but apply only apply to the underlying tables of specific entities?
You can disable table diffs generation for a specific entity using an attribute:
<cf:entity name="Customer" cfps:produceTableDiff="false">
<cf:property name="Id" key="true" />
</cf:entity>

Entity Framework EDMX Diagram table missing properties after update

I'm maintaining an asp.Net MVC 3 web application using Entity Framework 4.1 Database first approach. I have had to add two new properties to one of the tables in the database.
I then right clicked on the EDMX diagram and choose 'Update Model from Database' within the context menu, like so:
This works without any problems, but the two new fields aren't added to the expected table. However, when I open the EDMX file in XML format, I can see the two new fields listed, like so:
<EntityType Name="Shift">
<Key>
<PropertyRef Name="shiftID" />
</Key>
<Property Name="shiftID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
//Two new fields below
<Property Name="shiftTitleGradeID" Type="int" />
<Property Name="shiftTitleSubID" Type="int" />
</EntityType>
Can anyone advise me on how to get the two new fields into my EDMX diagram and not just the XML file?
Thanks in advance.
Got it fixed. I had to delete all references to the new properties in the XML file. Then recreate the update model from database procedure, this time ensuring the two checkboxes where ticked, Pluralize or singularize generated object names, and, Include foreign key columns in the model (I thought I had these ticked the first time, but I didn't).
This fixed my problem.

EF5: Multiple result sets from stored procedure

I followed this article to retrieve multiple entity sets from a stored procedure.
I have altered the column mappings of my entities so they pass through Resharper.
Do I have to rewrite the column mappings in the result mapping?
For example I have the following EntitySetMapping:
<EntitySetMapping Name="IrmaObjectConfiguraties">
<EntityTypeMapping TypeName="IrmaModel.IrmaObjectConfiguratie">
<MappingFragment StoreEntitySet="IrmaObjectConfiguratie">
<ScalarProperty Name="Gid" ColumnName="GID" />
<ScalarProperty Name="IrmaObjectGid" ColumnName="IrmaObject_GID" />
<ScalarProperty Name="IrmaConfiguratieGid" ColumnName="IrmaConfiguratie_GID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
This entity is returned bij as part of the stored proc result sets:
<ResultMapping>
<EntityTypeMapping TypeName="IrmaModel.IrmaObjectConfiguratie">
<!--ScalarProperty Name="Gid" ColumnName="Gid" />
<ScalarProperty Name="IrmaObjectGid" ColumnName="IrmaObject_Gid" />
<ScalarProperty Name="IrmaConfiguratieGid" ColumnName="IrmaConfiguratie_Gid" /-->
</EntityTypeMapping>
</ResultMapping>
When I remove the remarks the execution of the imported function goes ok, but when there is no column mapping I retrieve the following error:
The data reader is incompatible with the specified
'IrmaModel.IrmaObjectConfiguratie'. A member of the type,
'IrmaObjectGid', does not have a corresponding column in the data
reader with the same name.
As a workaround I could define all column mappings again, but is is also possible to use the column mappings that are defined in the EntitySetMapping?
I don't think ResultMapping re-uses mappings from EntitySet mappings. Seems (I am not 100% positive here) that if you don't specify property <-> column mappings EF will try to use property names as column names in the resultset returned by your stored procedure.

Distinguishing between storage model and conceptual model field names (Entity Framework)

Every sample I come across has the entities and properties in the storage model named exactly the same as in the conceptual model. So in the mapping section, I can't tell whether an entity or property is from the storage model or conceptual model.
This is a snippet of an Entity Framework diagram. Which "ID" fields are from the database and which are from the entities?
<AssociationSetMapping Name="FK_Orders_Customers" TypeName="ContosoModel.FK_Orders_Customers" StoreEntitySet="Order">
<EndProperty Name="Customer">
<ScalarProperty Name="ID" ColumnName="CustomerID" />
</EndProperty>
<EndProperty Name="Order">
<ScalarProperty Name="ID" ColumnName="ID" />
</EndProperty>
</AssociationSetMapping>
Well only databases have Columns, so ColumnName is the Database Name.
Name is from the Entity (or in this case the Association).
Hope this helps
Alex

Error 3023 using Entity Framework

Using the Entity Framework, I've modeled a fairly simple database schema with an ever-so-slightly more complex class hierarchy. In two places, I'm using single table inheritance with a single NVARCHAR(20) NOT NULL discriminator column. In one of those two places, it works great, no issues. But in the other place, with an almost identical pattern, I get the following error:
Error 3023: Problem in Mapping Fragments starting at lines 371, 375, 379, 382: Column MediaStream.MediaStreamTypeID has no default value and is not nullable. A column value is required to store entity data.
An Entity with Key (PK) will not round-trip when:
((PK does NOT play Role 'MediaStream' in AssociationSet 'FK_MediaStream_SessionID' OR PK is NOT in 'MediaStream' EntitySet OR Entity is type [SlideLinc.Model].MediaStream) AND (PK plays Role 'MediaStream' in AssociationSet 'FK_MediaStream_SessionID' OR PK is NOT in 'MediaStream' EntitySet OR Entity is type [SlideLinc.Model].MediaStream) AND (PK plays Role 'MediaStream' in AssociationSet 'FK_MediaStream_SessionID' OR PK is in 'MediaStream' EntitySet))
Here's the table definition (not including various indexes, foreign keys, etc.):
CREATE TABLE [dbo].MediaStream(
[MediaStreamID] UNIQUEIDENTIFIER NOT NULL,
[SessionID] UNIQUEIDENTIFIER NOT NULL,
[RtmpUri] nvarchar(250) NOT NULL,
[MediaStreamTypeID] nvarchar(20) NOT NULL,
CONSTRAINT PK_MediaStream PRIMARY KEY CLUSTERED
(
[MediaStreamID] ASC
)
I'm using the MediaStreamtypeID column as the discriminator: if it's set to "video", a VideoMediaStream class should be created, and if it's set to "audio", an AudioMediaStream class should be created.
The relevant portions of the EDMX file look like this:
<EntitySetMapping Name="MediaStream">
<EntityTypeMapping TypeName="IsTypeOf(SlideLinc.Model.MediaStream)">
<MappingFragment StoreEntitySet="MediaStream">
<ScalarProperty Name="RtmpUri" ColumnName="RtmpUri" />
<ScalarProperty Name="MediaStreamID" ColumnName="MediaStreamID" /></MappingFragment></EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SlideLinc.Model.VideoMediaStream)">
<MappingFragment StoreEntitySet="MediaStream" >
<ScalarProperty Name="MediaStreamID" ColumnName="MediaStreamID" />
<Condition ColumnName="MediaStreamTypeID" Value="video" /></MappingFragment></EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SlideLinc.Model.AudioMediaStream)">
<MappingFragment StoreEntitySet="MediaStream" >
<ScalarProperty Name="MediaStreamID" ColumnName="MediaStreamID" />
<Condition ColumnName="MediaStreamTypeID" Value="audio" /></MappingFragment></EntityTypeMapping></EntitySetMapping>
<AssociationSetMapping Name="FK_MediaStream_SessionID" TypeName="SlideLinc.Model.FK_MediaStream_SessionID" StoreEntitySet="MediaStream">
<EndProperty Name="MediaStream">
<ScalarProperty Name="MediaStreamID" ColumnName="MediaStreamID" /></EndProperty>
<EndProperty Name="Session">
<ScalarProperty Name="SessionID" ColumnName="SessionID" /></EndProperty></AssociationSetMapping>
So there are multiple things about this error that I don't get:
(1) Why does exactly this same approach work for my other class hierarchy, but not this one? I thought it might be the Entity Designer getting confused, so I deleted this portion of my hierarchy (in the XML), and recreated it, but I'm still getting it. I could try recreating the whole damn thing, but hell, that's a lot of work, and if I'm gonna have to be doing this very often, that doesn't leave a great taste in my mouth about the entity framework.
(2) What is it complaining about in the first place? I don't get how MediaStreamTypeID (which isn't a member of the primary key) has anything to do with the primary key at all, or why the fact that it can't be null is a problem (especially given that this same setup works elsewhere in my model!).
Any thoughts or suggestions?
I had a similar problem, and was able to solve it by setting the "Abstract" property for the base class to "True" and by removing the discriminator column from the base class in the model (either in the *.edmx file or in the designer view within Visual Studio).
I got the exactly same error but maybe with different reason from yours.
In the code below, I mis-copied some codes so 2 of inherited classes (LeveledItem and Team) are of the same "Type".
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new ScrumDbContextInitializer());
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<LeveledItem>()
.Map<LeveledItem>(m => m.Requires("Type").HasValue(typeof(LeveledItem).Name))
.Map<Team>(m => m.Requires("Type").HasValue(typeof(LeveledItem).Name))
.Map<Story>(m => m.Requires("Type").HasValue(typeof(Story).Name))
.Map<Task>(m => m.Requires("Type").HasValue(typeof(Task).Name)) .Map<Sprint>(m => m.Requires("Type").HasValue(typeof(Sprint).Name));
After the second one was changed to "typof(Team).Name", the error was fixed.
I had a similar problem to this, just out of interest, does deleting the .EDMX file and recreating it from scratch solve your problem?
What caused the problem:
Created a set of tables with some 0..* mappings
Generated the Entity Framework class, customised a whole bunch of Navigation properties
Went back to the DB and changed the cardinality of some of the 0..* to 1..* relationships - ie: set some of the FKs to !nullable
Updated the Entity Framework
Recompiled and BOOM I got a compilation error similar to yours
There seems to be an issue where the "Update Model from Database" command doesn't update the changed relationships correctly.
The solution was to open the EDMX file, look for the <Association Name="FK_XXX_XXX"> elements that were generated the first time and change the Multiplicity attribute on the relevant End point from Multiplicity="0..1" to Multiplicity="1"
I encountered this error when I inadvertently mapped 2 classes to the same table via the [Table] attribute (same effect via modelBuilder ToTable())