Error 3023 using the Entity Framework

Using the Entity Framework, I modeled a fairly simple database schema with an even more complex class hierarchy. In two places, I use unidirectional inheritance with a single column of the NVARCHAR (20) NOT NULL discriminator. In one of these two places it works great, no problem. But elsewhere, with an almost identical pattern, I get the following error:

Error 3023: problem with displaying fragments starting from lines 371, 375, 379, 382: Column MediaStream.MediaStreamTypeID has no default value and is not NULL. A column value is required to store entity data. An object with a key (PK) will not be rounded if: ((PK does not play the role of "MediaStream" in the AssociationSet "FK_MediaStream_SessionID" OR PK is not included in the "EntitySet" of MediaStream. EntityStyle is a type of [SlideLinc.Model] .MediaStream) AND ( The PC plays the role of "MediaStream" in the AssociationSet "FK_MediaStream_SessionID" OR PK is not included in the "EntitySet" of MediaStream. OR Entity is a type of [SlideLinc.Model]. MediaStream) And (PK plays the role of "MediaStream" in AssociationSet "FK_MediaStream_SessionID" or PK is in the "EntitySet" MediaStream))

Here's the definition of the table (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 use the MediaStreamtypeID column as a discriminator: if it is set to “video”, the VideoMediaStream class must be created, and if it is set to “sound”, the AudioMediaStream class must be created.

The relevant parts of the EDMX file are as follows:

  <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 many things about this error that I am not getting:

(1) Why is this exact approach working for my other class hierarchy, but not for this? I thought this might be the Entity Design constructor confused, so I deleted this part of my hierarchy (in XML) and recreated it, but I still get it. I could try to recreate this whole damn thing, but damn it a lot of work, and if I have to do it very often, it doesn't leave me a good taste in essence.

(2) What is he complaining about first? I don’t understand how MediaStreamTypeID (which is not a member of the primary key) has anything to do with the primary key at all or why the fact that it cannot be null is a problem (especially when you consider that the same setting works elsewhere my model!).

Any thoughts or suggestions?

+4
source share
3 answers

I had a similar problem, and I was able to solve it by setting the Abstract property for the base class to True and removing the discriminator column from the base class in the model (either in the * .edmx file or in the designer view in Visual Studio).

+2
source

I had a similar problem, just out of interest, did I delete the .EDMX file and recreate it from scratch, solving your problem?

What caused the problem:

  • Created a set of tables with some 0 .. * mappings
  • Created an Entity Framework class, configured a bunch of navigation properties
  • He returned to the database and changed the power of some of the relations 0 .. * to 1 .. * - that is: set some of FK to a value! nullable
  • Updated Entity Framework
  • Recompiled and BOOM I have a compilation error similar to yours.

It seems that the problem is that the “Update Model from Database” command does not 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 for the first time, and change the Multiplicity attribute at the corresponding endpoint from Multiplicity="0..1" to Multiplicity="1"

0
source

I got exactly the same error, but maybe for various reasons. In the code below, I copied some codes incorrectly, so the 2 inherited classes (LeveledItem and Team) have 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 was changed to "typof (Team) .Name", the error was fixed.

0
source

Source: https://habr.com/ru/post/1286217/


All Articles