This is a migrated thread and some comments may be shown as answers.

mapping scalar value function using fluent map api

1 Answer 86 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ezequiel
Top achievements
Rank 1
Ezequiel asked on 18 Mar 2013, 04:49 PM
I have a function in Mysql, that function returns a DateTime, i need map that function using fluent map api.. there is a way?
regards.

1 Answer, 1 is accepted

Sort by
0
Kaloyan Nikolov
Telerik team
answered on 19 Mar 2013, 04:10 PM
Hello Ezequiel,

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

In LINQ statements (documentation)
Example: 

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.

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


Invoking from the context
Example:
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;
    }
    else
    {
        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.


Regards,
Kaloyan Nikolov
the Telerik team
Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
Tags
Development (API, general questions)
Asked by
Ezequiel
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Share this question
or