Search code examples
servicestackormlite-servicestack

ServiceStack.OrmLite: Table collision when class name appears in different namespaces


When having two classes that has the same name, but in different namespaces, ServiceStacks OrmLite is unable to distinguish between the two. For example:

Type type = typeof(FirstNameSpace.BaseModel);
using (IDbConnection db = _dbFactory.Open())
{
     db.CreateTable(false, type); // Creates table "basemodel"
}
                        
type = typeof(SecondNamespace.BaseModel);
using (IDbConnection db = _dbFactory.Open())
{
     db.CreateTable(false, type); // Creates nothing as there already is a table 'basemodel', even though its a completely different object/class
}
                        

Is there a general, clean way to make sure that this is resolved?

It is not ideal to be forced to name classes uniquely; a part of the namespaces in .NET is to group and categorize different classes. Also, there might be third-party assemblies with the same class names, that is not available to change for you.

Is there a way to handle this?


Solution

  • OrmLite uses the name of the Type for the table name so you can’t use 2 different Types with the same name.

    You will need to either rename one of the Types to avoid the collision or use the [Alias(“UseTableName”)] attribute to tell one of the Types to use a different RDBMS Table name.