I am evaluating DO and wanted to know if there is a way to use TableMapping attribute to specify the name of the table used to manage the one-to-many relationship between two entities.

E.g if you have an entity 'Customers' and another 'Orders', then you define a field 'CustomerOrders' of type EntitySet(Of Orders) in Customers to reference the orders of a particular customer...DO creates a link table called Customers-CustomerOrders-Orders to keep track of the orders for each customer in the database.

How can I rename that table to something like CustomerToOrders?

The reason I ask is querying. I want friendly table names so I can write plain SQL queries against the database (e.g. for reporting).

On a related note, I am choosing DO because of the "code first" approach which allows me to mostly ignore the database. However, when you start querying the database directly, the schema begins to matter. What is the best practice here? How do you handle ad-hoc querying when you can't use LINQ?

Many thanks, Eric.

asked Feb 19 '14 at 06:14

ericmutta's gravatar image


One Answer:

Hello Eric,

there are two options for you. Use your own hand written many-to-many association with custom element type. This obviously would allow you to apply any name to this table, however this approach might be less convenient.

Another option is to utilize DomainConfiguration.NamingConvention.NamingRules setting. For example you can use NamingRules.UnderscoreHyphens flag to replace hyphens in table names. In your example table would be called Customer_CustomerOrders_Orders which allows to use it without extra quoting.

Generally DO generates tables and columns with the same names as corresponding types and properties in the code. If you want to use hand-written SQL it's recommended to enable both UnderscoreHyphens and UnderscoreDots flags to avoid quoting tables and columns in SQL. Dots are used in nested field names (e.g. for columns of composite foreign key) and hyphens are used for naming tables for many-to-many associations as you've already figured.

answered Feb 19 '14 at 06:42

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov

Many thanks Dennis, for the fast response and for a solution that solves the problem!

I am definitely liking what I am seeing in DO so far, I get the feeling you guys have put a lot of thought into this thing over the years!

(Feb 19 '14 at 07:14) ericmutta ericmutta's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA