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.
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.
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.
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.
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With