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.
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.
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
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.
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 clrudolphi on Dec 12, 2008 01:55 PM
clrudolphi
Member
5 Points
4 Posts
Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 03:57 PM|LINK
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
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 07:08 PM|LINK
One work around is renaming the entity in the CSDL (and fix up the mapping from the logical layer) for the duplicate named tables.
clrudolphi
Member
5 Points
4 Posts
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 07:49 PM|LINK
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
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 07:57 PM|LINK
[
DisplayName("Address")]ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 08:00 PM|LINK
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.
clrudolphi
Member
5 Points
4 Posts
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 10:03 PM|LINK
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
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 10:10 PM|LINK
Great job Chris. Thanks for posting the solution.
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 11, 2008 10:40 PM|LINK
Yes, very nice indeed! I actually ran into this issue before, but what I had come up with was not as clean.
thanks,
David
davidebb
Contributor
7006 Points
1366 Posts
Microsoft
Re: Registering Multiple Models/Contexts; Table.ListActionPath gets confused
Dec 12, 2008 01:51 AM|LINK
Ok, I stole your trick and made a blog post out of it!
David