views:

103

answers:

1

Hi folks,

We have a simple Table per Type Entity Framework 4.0 model :-

alt text

  • ALl classes are all POCO's.
  • Post class is abstract.
  • Discussion and List are classes are concretes, that inherit's from Posts (as shown in the diagram).

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

Posts.AddObject(discussion);

And the Sql Server syntax is in two parts. The second one errors. Notice the sql schema namespace? why is that? (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 */)

Notice the table name is [XWingModelStoreContainer].[Discussions] ?? Shouldn't that be [dbo].[Discussions] ?? How can we fix this, please?

.

.

UPDATE:

Also, here's another screen shot of the properties of our designer .. so u can see that we thought that it should be calling [dbo] as that is the default Database Scheme Name ..

alt text

and in the Xml edmx file.. there's two name'd lines..

<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 ..

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

Does this also help? I have no idea how those names got there (i'm assuming autogenerated) and how do i need to change them? If so, it looks like it's only possible via the xml file .. which is fine .. but feels ... wrong?

Cheers :)

+1  A: 

I was having a similar issue, and the problem was solved by making sure the child tables have the Primary Key set.

Even though the child tables have an FK to the parent table, you still need to set these fields as the PK, otherwise EF does not know how to execute the INSERT on the child tables.

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

Give that a go.

RPM1984
Win :) that was it ;) Awesome!! Thanks!
Pure.Krome