Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using schema names with SQL Server & ServiceStack.OrmLite

Anyone know how to apply the correct Alias attribute to query tables with schema names?

I have a table called accounts.register. I've tried using [Alias("accounts.register")] as the class decorator attribute for Register class but this doesn't work.

If I change the schema to dbo then I can remove the alias and everything works. Unfortunately I have a legacy system with many schemas so I need this to work.

like image 440
Neil Dobson Avatar asked Dec 19 '12 04:12

Neil Dobson


People also ask

What is schema name in SQL Server?

What is Schema in SQL? In a SQL database, a schema is a list of logical structures of data. A database user owns the schema, which has the same name as the database manager. As of SQL Server 2005, a schema is an individual entity (container of objects) distinct from the user who constructs the object.

How do I select a SQL schema?

It explains that from sql server 2005 onwards you can set the default schema of a user with the ALTER USER statement. Unfortunately, that means that you change it permanently, so if you need to switch between schemas, you would need to set it every time you execute a stored procedure or a batch of statements.

What is database schema name?

A schemaName represents a schema. Schemas contain other dictionary objects, such as tables and indexes. Schemas provide a way to name a subset of tables and other dictionary objects within a database. You can explicitly create or drop a schema.


1 Answers

OK I figured it out. Along with the Alias attribute is the Schema attribute. The former is in the ServiceStack.DataAnnotations namespace but the latter is in the ServiceStack.OrmLite namespace. Here's an example to map fields field1 & field2 to/from myschema.mytable:

using System;
using ServiceStack.OrmLite;
using ServiceStack.DataAnnotations;

[Schema("myschema")]
[Alias("mytable")]
public class MyEntity
{
    [PrimaryKey]
    [AutoIncrement]
    public long Id { get; set; }

    [Alias("field1")]
    public string SomeField1 { get; set; }

    [Alias("field1")]
    public string SomeField2 { get; set; }
}
like image 56
Neil Dobson Avatar answered Oct 14 '22 11:10

Neil Dobson