Using schema names with SQL Server & ServiceStack.OrmLite
Asked Answered
E

1

12

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.

Entwine answered 19/12, 2012 at 4:36 Comment(0)
E
22

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; }
}
Entwine answered 19/12, 2012 at 5:39 Comment(2)
Any idea how to get OrmLite to do this if you're using the T4 POCO generation?Kish
@Kish support added recently in version 4. github.com/ServiceStack/ServiceStack.OrmLite/commit/… . If you want to add it yourself, you can add '[Schema("<#=tbl.Schema#>")]' to the line below '[Alias("<#=tbl.Name#>")]' and that will add the Schema attribute you require.Concession

© 2022 - 2024 — McMap. All rights reserved.