Entity Framework mapping views to entities error

Posts   
 
    
nathan.liu
User
Posts: 11
Joined: 09-Jul-2010
# Posted on: 01-Aug-2010 01:07:19   

Hi

When I map a very simple view (e.g. constructed from two tables with a 1 to 1 relationship) to an entity I get the following validation error:

The entity 'MyViewTestEntity' is mapped onto the target '.dbo.My_View_Test', which is a view. The Entity Framework requires a valid, unique PK to be defined on the target of a mapping and with a view this can't be guaranteed therefore this construct isn't supported on the Entity Framework

This error persists even when I set a PK for 'MyViewTestEntity'. It seems to suggest that its impossible to map views to entities using LLBLGen Pro Entity Framework? Is this so?

I'm using LLBLGen Pro V3 with VS 2010 (C#) and SQL Server 2005.

Trying to generate using SD.EntityFramework.v4 and SD.EntityFramework.v4(self tracking POCO entities)

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 01-Aug-2010 11:13:21   

The problem is that if you specify a pk, it's for the entity definition, but the view definition (it's columns etc.) are defined in the edmx file as well, and the EF requires that that definition also has a pk. As it's not always possible to define a pk on a view (i.e.: pick a nonnullable column), the feature is not supported at this point.

Frans Bouma | Lead developer LLBLGen Pro
nathan.liu
User
Posts: 11
Joined: 09-Jul-2010
# Posted on: 01-Aug-2010 12:54:04   

thanks for the quick reply Otis. But this is bad news.

Is there any way I can access a DB view in my model then? Are typed lists my only alternative?

arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 01-Aug-2010 14:20:54   

As it's not always possible to define a pk on a view (i.e.: pick a nonnullable column), the feature is not supported at this point.

When the designer has defined a pk on the view , why not emit that into the edmx?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 01-Aug-2010 20:48:56   

arschr wrote:

As it's not always possible to define a pk on a view (i.e.: pick a nonnullable column), the feature is not supported at this point.

When the designer has defined a pk on the view , why not emit that into the edmx?

That only works if there's 1 entity mapped onto the view. But indeed, it could work, in that particular situation, we could test for that particular case and if it fits, accept it, otherwise throw an error. We'll look into that, to see if this works. It still could give problems though, if the field isn't unique. That's also why entities targeting a view don't require a PK.

It's however advisable to avoid EF with views at this point, as it's not designed to use them at this point: the native EF designer for example simply groups all non-nullable fields in the 'key' (which could be all fields!) which still might not be sufficient.

Frans Bouma | Lead developer LLBLGen Pro
nathan.liu
User
Posts: 11
Joined: 09-Jul-2010
# Posted on: 02-Aug-2010 15:05:21   

Well we're already heavily invested in EF, so there's no turning back on that now.

arschr's suggestion would solve our immediate problems. Hope an update comes out soon.

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 02-Aug-2010 15:54:08   

We'll look into this tomorrow (tuesday) to see if we can build this in. No guarantees if it will work in the end, but we will try to make some or one situation work and filter out the others which don't work with the validator.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 03-Aug-2010 15:10:38   

We have to postpone this to tomorrow (Wednesday).

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39903
Joined: 17-Aug-2003
# Posted on: 04-Aug-2010 15:36:13   

We managed to make the change work simple_smile Thanks to Arschr for the idea simple_smile

Attached you'll find two files: the validator (for v1, which is re-used in v4, so works for v4 as well) and the edmx include template which generates the edmx file.

place the validator in the folder: (as administrator!) <llblgen pro installation folder>\Frameworks\Entity Framework overwriting the existing dll

place the edmx include template in the folder (as administrator!) <llblgen pro installation folder>\Frameworks\Entity Framework\Templates\Shared\Shared

Entities mapped onto a view are now supported, as long as each entity mapped onto a view has a valid pk and if you map multiple entities onto the same view the target fields for pk fields are all the same for all entities mapped onto the same view.

The next build of the designer will have this change included as well. If you run into problems, please post in this thread again to re-open it.

Attachments
Filename File size Added on Approval
EF_EntityOnViewHotFix.zip 16,394 04-Aug-2010 15:36.48 Approved
Frans Bouma | Lead developer LLBLGen Pro