implement repository in asp.net mvc 5

Toxic

I got dbset for table Functions in database and FunctionsContext: dbContext. I am implementing repository. In my interface I have only one function at the movement "GetFunctions". I got stuck in implementing class; method "GetFunctions" where I need to call FunctionsContext to get all list of available functions title from database and then send to controller class

I am using mvc5 asp.net and entity framework

dbContext

public class FunctionsContext : dbContext
{
    public DbSet<App_Functions> Functions { get; set; }
}

model

[Table("Functions")]
public class App_Functions
{
    [Key]
    public int Function_ID { get; set; }

    [StringLength(50)]
    [Required]
    public string Title { get; set; }

    public int Hierarchy_level { get; set; }
}

Domain Class

public class Functions
{
    public Functions()
    {

    }

    public int Function_ID { get; set; }
    public string Title { get; set; }
    public int Hierarchy_level { get; set; }
}

IRepository

interface IFunctionRepository: IDisposable
{
    IQueryable<Functions> GetFunctions { get; }
}

IRepository Implementation class

public class FunctionRepository : IFunctionRepository
{
    private FunctionsContext fun_Context = new FunctionsContext();

    public IQueryable<Functions>GetFunctions
    {
      ?????????
    }
}

what I want to implement in IQueryableGetFunctions

using (var db = new FunctionsContext())
{
    var query = from b in db.Functions
                orderby b.Function_ID
                select b;

    foreach (var item in query)
    {
        var a2 = item.Title;
    }
}
Pavel Kutakov

I think the easiest way will be the following:

public IQueryable<Functions> GetFunctions()
{
    return fun_Context.Functions.Select(x=>new Functions {
                   Function_ID = x.Function_ID,
                   Title = x.Title,
                   Hierarchy_level = x.Hierarchy_level 
               });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related