Custom table name

Topics: Writing modules
May 3, 2011 at 9:58 AM

Hi! I have a problem.

I using MS SQL server. I'm create module (for example "Commerce") and table "Commerce_ProductRecord" was create in database automatically.

But I want use table with name "Product" for my custom module.

Is this posible?

If yes, can you give me link or example code? Because I can't find this in documentation.

May 3, 2011 at 1:33 PM

The reason for this is to vastly reduce the likelihood of conflicts with other modules.

What if another module decided to try and add a "Product" table?

May 3, 2011 at 1:53 PM

I understand that this may conflict. I'm as developer want to use at own risk
and I know what I want

May 3, 2011 at 2:10 PM

Is there a specific reason you need the name like this? Perhaps there's another way to solve the same problem...

May 3, 2011 at 2:29 PM
Edited May 3, 2011 at 2:30 PM

Yes. For example I need use specific table, with fixed name. And I can't change name. It does not depend on my web part. But I want use orchard for make interface for add, edit and display information from this table.

  Just I want to know, this is possible or not?

  If this possible. Maybe you can give me some example or link to the documentation?

May 3, 2011 at 3:47 PM

I believe it's a convention within Orchard that forces you to inherit from ContentPartRecord.

Inside a Migration you could create a table with whichever name you want. And you should be able to write the ContentDriver for a content type. However you will have to use a dummy ContentPartRecord, and execute the actual SQL/NHibernate code instead of what Orchard does behind the scenes via ContentPartRecord.

Note that by using your own table you will have to integrate somehow the records you have in that table with the dummy ContentPartRecord you used in orchard.

The way Orchard works is that you create a ContentRecord for persistence. The base class called ContentPartRecord has a set of properties that orchard uses to join with other ContentRecord tables when it is asked for a specific ContentItem. 

So, you can use the base mechanics required by orchard and maybe save a id/guid to link to another table. Maybe add a 1 to 1 relationship between the ContentPart defined in Orchard and your own Product table. I envision something like:

public class YourContentRecord: ContentPartRecord{

public Product Product{get;set;}


where product is mapped by hand somehow via NHibernate to whichever table you need.

My opinion is that you should try and use Orchards internal mapping and leave it at that. However if you really need it, you should be able to use NHibernate specific methods to also load the extra data you need. The way I'm suggesting is to use YourContentRecord as an Adapter for the real data you need from the ProductTable.


Does it make sense now ?

May 3, 2011 at 4:15 PM

Ok. I understand you. Thank you very much.
It seems take long time. I maybe will write own web site it's will be better. I need think.

May 3, 2011 at 4:28 PM

You can create records and use IRepository<TRecord> to access them, without them being ContentPartRecords. But they will still follow the modular naming convention.

For something like a Products table you probably do want to build a ProductPart anyway, so you can easily use the rest of Orchard's content management.

On the other hand if you don't want to use Orchard's features then you're better off just building your own website certainly :)

Jan 27, 2012 at 8:13 AM
Edited Jan 27, 2012 at 8:14 AM

A different question, but slightly related;

The current naming convention that Orchard uses makes for not so pretty table names.
I of course understand it's to avoid naming conflicts between the modules.

But would it be possible in a future version to use database schema's as a prefix for the table? Right now, dbo is used, but what if a new schema would be created based on the module's name? Like: orchard_framework for the Orchard.Framework module. Then a full table name would be: orchard_framework.ContentItems (instead of ContentItemRecord). Even better would be to configure a data entity to use a specific table name, perhaps via an Attribute.

I understand that it has nothing to do with technically improving Orchard, it's just a matter of backend aesthetics which would be nice to improve :)