locked
Registering Multiple Models/Contexts; Table.ListActionPath gets confused RRS feed

  • Question

  • User1085923623 posted

    I have modified the stock Default.aspx to support multiple data contexts, using techniques suggested elsewhere in this forum.  The page correctly pulls back the correct Metamodel (I use a querytstring to indicate which database is to be displayed).

     I have set up routes per context that includes the database name as part of the path, so that the routing mechanism can return the correct model.

     The GridView that binds to the dynamic data source correctly displays all of the tables for which scaffolding has been enabled.

    The problem is that the call to Eval("ListActionPath") returns the wrong URL when the databases have duplicate names.  In that case, the ListActionPath always points to the first database that was registered with that table name.

     Is this a known issue?

    Any work-arounds? 

    Thanks,
    Chris

    Thursday, December 11, 2008 11:57 AM

Answers

  • User1085923623 posted

    Problem solved and as always, Reflector is your friend.

     I changed my solution so that the database name is a parameter of the route, as in "/{dbname}/{table}/{action}.aspx", and also that the route registration included an additional constraint, so that when registering the metamodel for DB1, the route constraints not only include the Action constraints, but also a constraint on the value of {dbname}, as in:

    routes.Add(New DynamicDataRoute("DB/{dbname}/{table}/{action}.aspx") With { _
                .Constraints = New RouteValueDictionary(New With {.Action = "List|Details|Edit|Insert|SearchList", .dbname = "DB1"}), _
                .Model = DB1_model})

    The default.aspx was modified to expect a query string parameter with the name "dbname" which I manually use to get the MetaModel using a dictionary of dbnames to datacontext types.  Since the query string parameter matches the route structure, the routing system automatically plugs that value into its route matching logic.

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2008 6:03 PM

All replies

  • User-1005219520 posted

    One work around is renaming the entity in the CSDL (and fix up the mapping from the logical layer) for the duplicate named tables.

    Thursday, December 11, 2008 3:08 PM
  • User1085923623 posted

    I'm using Linq2SQL, not EF, but I get your point.

     

    In my case there are simply too many collisions for that to be a feasible solution.  This app is a simple scaffolding app over a set of databases that make up another application - for the purposes of admin support.  Organizationally, we use some conventions that result in a few tables being present in each database so elimination of the naming collisions via renaming the tables in the mapping layer would be an expensive, and likely confusing for the admins anyway since they expect to see tables with specific names.

     

    Chris

    Thursday, December 11, 2008 3:49 PM
  • User-1005219520 posted
    It only takes a second to right click an entity in the designer, rename it - then add the displayName attribute to name it back. You can do the same thing in L2S

    [DisplayName("Address")]

    Thursday, December 11, 2008 3:57 PM
  • User-1005219520 posted

    I would rename the the tables as DB_Table (SQL Server names tables a Schema.Table)  - that would remove all ambiguity. That way there is no question on what DB the table came from.

    Thursday, December 11, 2008 4:00 PM
  • User1085923623 posted

    Problem solved and as always, Reflector is your friend.

     I changed my solution so that the database name is a parameter of the route, as in "/{dbname}/{table}/{action}.aspx", and also that the route registration included an additional constraint, so that when registering the metamodel for DB1, the route constraints not only include the Action constraints, but also a constraint on the value of {dbname}, as in:

    routes.Add(New DynamicDataRoute("DB/{dbname}/{table}/{action}.aspx") With { _
                .Constraints = New RouteValueDictionary(New With {.Action = "List|Details|Edit|Insert|SearchList", .dbname = "DB1"}), _
                .Model = DB1_model})

    The default.aspx was modified to expect a query string parameter with the name "dbname" which I manually use to get the MetaModel using a dictionary of dbnames to datacontext types.  Since the query string parameter matches the route structure, the routing system automatically plugs that value into its route matching logic.

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 11, 2008 6:03 PM
  • User-1005219520 posted

    Great job Chris. Thanks for posting the solution.

    Thursday, December 11, 2008 6:10 PM
  • User1641955678 posted

    Yes, very nice indeed!  I actually ran into this issue before, but what I had come up with was not as clean.

    thanks,
    David

    Thursday, December 11, 2008 6:40 PM
  • User1641955678 posted

    Ok, I stole your trick and made a blog post out of it!

    David

    Thursday, December 11, 2008 9:51 PM