Why is this Entity Framework code not stored in the database?

We have a simple table for the Type Entity Framework 4.0 model: -

alt text

  • ALl classes are all POCO.
  • Class post abstract .
  • Discussion and List are concretes classes that inherit from Posts (as shown in the diagram).

When we try to keep Discussion , we do the following code: -

Posts.AddObject(discussion);

And the Sql Server syntax consists of two parts. The second mistake. Pay attention to sql schema namespace? Why is this? (Code taken from EFProf )

 insert [dbo].[Posts] ([Subject], [UniqueSubject], [Content], [CreatedOn], [ModifiedOn], [IsVisible], [UserId]) values('Test Subject' /* @0 */, 'sdfsdfsdfsdfsfdssd' /* @1 */, 'this is a lot of content - pew pew pew' /* @2 */, '23/09/2010 12:22:08 PM +10:00' /* @3 */, '23/09/2010 12:22:08 PM +10:00' /* @4 */, 1 /* @5 */, 1 /* @6 */) select [PostId] from [dbo].[Posts] where @@ROWCOUNT > 0 and [PostId] = scope_identity() insert [XWingModelStoreContainer].[Discussions] ([PostId]) values(20132 /* @0 */) 

Note that the table name is [XWingModelStoreContainer]. [Discussions] ?? It should not be [dbo]. [Discussions] ?? How can we fix this, please?

.

.

UPDATE:

In addition, here is another snapshot of our designer’s properties .. so you can see that we thought he should call [dbo], since this is the default Database Scheme Name ..

alt text

and in the Xml file edmx .. there are two lines of name.d.

 <edmx:StorageModels> <Schema Namespace="XWingModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com.. snip ..." xmlns="http://schemas.microsoft.com/ ..snip .."> <EntityContainer Name="XWingModelStoreContainer"> ..... 

and..

 <!-- CS mapping content --> <edmx:Mappings> <Mapping xmlns="http://schemas.microsoft.c.. snip .." Space="CS"> <Alias Key="Model" Value="XWingModel" /> <Alias Key="Target" Value="XWingModel.Store" /> <EntityContainerMapping CdmEntityContainer="XWingEntities" StorageEntityContainer="XWingModelStoreContainer"> ....... 

Does it help? I have no idea how these names got (I assume auto-generation) and how do I need to change them? If so, it seems that this is only possible through an XML file .. this is normal .. but it feels ... wrong?

Greetings :)

+4
source share
1 answer

I had a similar problem, and the problem was solved by using a primary key in the child tables .

Even if the child tables have FK in the parent table, you still need to set these fields as PK, otherwise EF does not know how to perform INSERT in the child tables.

So, in your example, try setting PostId as the primary key in the Discussion table, and then update your model.

Give it back.

+1
source

All Articles