Mapping Entities to a View

Posts   
 
    
Posts: 112
Joined: 09-Aug-2004
# Posted on: 27-Oct-2008 14:32:32   

I came across Walaa's suggestion in this thread

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5528

where he says

1- Create a database View that Union(s) all your LookUp Tables (hence preserving the Refrential Integrity they have with another tables)

2- This view will have a new discriminator column that differentiate between values from each table.

3- Map the view to an Entity or a TypedView

4- You will need to pass the discrminating value to your Fetch Logic method to filter upon.

Is it possible to set this up so you can save/edit entities? My view was initially looking like this


SELECT   Id, Name, Description, 1 AS 'LookupType'
FROM         dbo.Lookup1
UNION
SELECT   Id, Name, Description, 2 AS 'LookupType'
FROM         dbo.Lookup2
UNION
SELECT   Id, Name, Description, 3 AS 'LookupType'
FROM         dbo.Lookup3
UNION
SELECT   Id, Name, Description, 4 AS 'LookupType'
FROM         dbo.Lookup4

This works great for fetching, but since I am embedding the discriminator value in the select, any updates fail.

Is there any way to have one view which fetches all values from multiple tables while still maintaining the ability to make edits and insertions?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-Oct-2008 14:47:23   

I came across Walaa's suggestion in this thread

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=5528

My words is going to hunt me forever simple_smile

Why don't you insert in the tables directly using entities mapped on these tables?

Posts: 112
Joined: 09-Aug-2004
# Posted on: 27-Oct-2008 14:54:26   

Walaa wrote:

My words is going to hunt me forever simple_smile

Why don't you insert in the tables directly using entities mapped on these tables?

Hehe, always! You should rename old posts to "Some guy"

I'd like to map the entities to the view so I can fetch all the lookups in one easy call. Is it possible to map an entity to both somehow? I'd like to work with one entity object which can be selected from the view, but act as if it is being updated and inserted into the table itself.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 27-Oct-2008 15:40:10   

I think you should map an entity to the view and this should be used for fetching only. And you may also map another entities to each of the underlying table for Save and Delete actions.

Also you can add a method to a partial class file of the view entity to return the corresponding table entity based upon the descriminating field.

So you can use this entity for Updates or Deletes.