Serenity

How to Use Joins in Serenity Using ForeignKey Attribute

How to Use Joins in Serenity Using ForeignKey Attribute

Joins in Serenity Platform

Due to relationships, we frequently do joins when searching databases. These joins tend to be LEFT or INNER joins.
Entities created using Serenity can be used in the same ways as SQL views, allowing you to add fields from other tables and query them as if they were one large merged table.


A. Scenario - if there is some master table like city master as its primary key in User Table

1. Show City Information in User Entity & User Form using LeftJoin Attribute


public class UserRow : Row
{
[Identity, PrimaryKey, [DisplayName("UserId")]
    public Int32? Id
    {
        get { return Fields.Id[this]; }
        set { Fields.Id[this] = value; }
    }

    [DisplayName("City"), ForeignKey("Cities", "Id"), LeftJoin("jc")]
    public Int32? CityId
    {
        get { return Fields.CityId[this]; }
        set { Fields.CityId[this] = value; }
    }

    [Expression("jc.[Name]")]
    public string CityName
    {
        get { return Fields.CityName[this]; }
        set { Fields.CityName[this] = value; }
    }
}

Here
- We are using LEFT JOIN join
- We assign an alias to Cities table as jc when it is connected.
- The join ON expression is jc. With some assistance from the ForeignKey property, jc.[Id] == T0.[CityId].
- CityName is a view field with the expression jc.Name, not a column in the User table. (CityName variable
came from the Name field of the Cities table).


2. Show Country Information in User Entity & User Form Where Country table is directly related with City instead of User


public class UserRow : Row
{
    ....

    ....

    [DisplayName("Country"), Expression("jc.[CountryId]"), ForeignKey("Countries", "Id"), LeftJoin("jct")]
    public Int32? CountryId
    {
        get { return Fields.CountryId[this]; }
        set { Fields.CountryId[this] = value; }    
    }

    [Expression("jct.[Name]")]
    public string CountryName
    {
        get { return Fields.CountryName[this]; }
        set { Fields.CountryName[this] = value; }
    }    
}

Here
- We defined a LEFT JOIN on CountryId field in Cities table.
- We assigned jct alias to Countries table and bring in the name field from it.



B. Scenario - if there User Table has referenced/Primary Key in another table as Foreign Key

Entity classes can additionally have the LeftJoin attribute attached to them. For Example User is Userd in in UserDetail Table and we want to show address from User detail table into User Entity


[LeftJoin("ud", "UserDetails", "ud.[UserId] = T0.[Id]")]
public class UserRow : Row
{
    ....

    ....

    [Identity, PrimaryKey, [DisplayName("UserId")]
    public Int32? Id
    {
        get { return Fields.Id[this]; }
        set { Fields.Id[this] = value; }
    }

    [Expression("ud.[CorrespondenceAddress]")]
    public string CorrespondenceAddress
    {
        get { return Fields.CorrespondenceAddress[this]; }
        set { Fields.CorrespondenceAddress[this] = value; }
    }
}








Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram