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?