none
Multiple views with same layout mapping to one entity RRS feed

  • Question

  • Hi,
    I have 2 views. These views have the same column layout. Lets call them ViewA and ViewB.
    These views are read only, there won't be any updating.
    Is it possible to map ViewB to the entity type ViewA, without generating entity type ViewB?
    In the designer, the mapping details for ViewB I set 'Maps To' to ViewA.
    The context generated is:
    public partial class MyEntities : DbContext
    {
    ...
        public DbSet<ViewA> ViewA { get; set; }
        public DbSet<ViewB> ViewB { get; set; }
    }
    

    I expected it would generate the following DbContext:
    public partial class MyEntities : DbContext
    {
    ...
        public DbSet<ViewA> ViewA { get; set; }
        public DbSet<ViewA> ViewB { get; set; }
    }
     
    • Edited by pexxx Tuesday, October 25, 2011 11:50 AM Code not readable, small font
    Tuesday, October 25, 2011 11:46 AM

Answers

  • pexxx,

    That's what I would expect it to do.  You can't map two entities to the same table.  In fact after you did the designer change and selected the mapping details for ViewB to map to ViewA it should have given a runtime error. 

    And even if you now change the DbContext to what you want (both ViewA and ViewB both pointing to ViewA) it will give an error. 

    The only way to do what you want to do, would be to create a Table Splitting between viewA and viewB where both have navigation properties to each other but ViewB has some of ViewA's columns and ViewA has the other columns. 

    You could also create a DefiningQuery, which is where you take the results of a query (which could be: select * from ViewA) and map it to an entity such as ViewB.  Then, you could in effect have ViewB mapping to ViewA in a roundabout way.


    Tom Overton
    • Marked as answer by pexxx Tuesday, October 25, 2011 1:37 PM
    Tuesday, October 25, 2011 1:19 PM

All replies

  • pexxx,

    That's what I would expect it to do.  You can't map two entities to the same table.  In fact after you did the designer change and selected the mapping details for ViewB to map to ViewA it should have given a runtime error. 

    And even if you now change the DbContext to what you want (both ViewA and ViewB both pointing to ViewA) it will give an error. 

    The only way to do what you want to do, would be to create a Table Splitting between viewA and viewB where both have navigation properties to each other but ViewB has some of ViewA's columns and ViewA has the other columns. 

    You could also create a DefiningQuery, which is where you take the results of a query (which could be: select * from ViewA) and map it to an entity such as ViewB.  Then, you could in effect have ViewB mapping to ViewA in a roundabout way.


    Tom Overton
    • Marked as answer by pexxx Tuesday, October 25, 2011 1:37 PM
    Tuesday, October 25, 2011 1:19 PM
  • Hi Tom,

    Table splitting will not result in an object model I would like.

    DefiningQuery would be nice, but since I need the Update Model from database, and according the MSDN, the Defining Query will be overwritten.

    I think I create two stored procs to query the views and have the result mapping to the same (new) complex type in an Function Import.

    I marked your reply as answer. It was not the answer I was hoping for, but at least now I know I can't map one entity to multiple views / tables.

    Thanks for your time.

    Tuesday, October 25, 2011 1:36 PM