mapping scalar value function using fluent map api

2 posts, 0 answers
  1. Ezequiel
    Ezequiel avatar
    1 posts
    Member since:
    Mar 2013

    Posted 18 Mar 2013 Link to this post

    I have a function in Mysql, that function returns a DateTime, i need map that function using fluent map api.. there is a way?
  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 19 Mar 2013 Link to this post

    Hello Ezequiel,

    With OpenAccess ORM you are able to work with database functions in both ways:

    In LINQ statements (documentation)

    var projects = from p in dbContext.Projects
              select new
                  ProjectId = p.Id,
                  Name = p.Name,
                  LastActivity = FluentModel.GetLastActivity(p.Id)
    To achieve that you should define a static method in your FluentContext equivalent of the database function you want to call. This method will not be called explicitly, it is only a marker for OpenAccess to translate the method call to a function call at runtime.

    [MappedFunctionAttribute(Name = "`GetLastActivity`", IsDeterministic = false, Backend = Backend.MySql)]
    public static System.DateTime GetLastActivity(int? Id)
        throw new NotImplementedException();

    Invoking from the context
    var date = dbContext.GetLastActivity(12);

    If you want to execute it that way you should use the OpenAccess ADO API to implement a non-static method in your fluent context class, like the example below:

    public DateTime GetLastActivity(int? Id)
        OAParameter parameteId = new OAParameter();
        parameteId.ParameterName = "@Id";
        if (Id.HasValue)
            parameteId.Value = Id.Value;
            parameteId.DbType = DbType.Int32;
            parameteId.Value = DBNull.Value;
        DateTime date = this.ExecuteScalar<DateTime>("SELECT `GetLastActivity`(@Id)", parameteId);
        return date;

    I hope that helps. Do not hesitate to contact us if you have other questions.

    Kaloyan Nikolov
    the Telerik team
    Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
  3. DevCraft banner
Back to Top