Converting function from Code Fluent to Entity Framework

54 Views Asked by At

I'm working in a project with Code Fluent but we need to migrate it to Entity Framework So I need help to convert this function or finding its equivalent maybe


 public static System.Data.DataSet GetSpaces(System.Guid userId)
    {
        if ((userId.Equals(CodeFluentPersistence.DefaultGuidValue) == true))
        {
            throw new System.ArgumentNullException("userId");
        }
        System.Data.DataSet ret = default(System.Data.DataSet);
        CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(Erpeo.Store.Model.Constants.Erpeo_Store_ModelStoreName).Persistence;
        persistence.CreateStoredProcedureCommand(null, "User", "GetSpaces");
        persistence.AddParameter("@userId", userId);
        System.Data.IDataReader reader = null;
        try
        {
            reader = persistence.ExecuteReader();
            if ((reader.Read() == true))
            {
                ret = CodeFluent.Runtime.CodeFluentPersistence.LoadDataSet(reader);
            }
        }
        finally
        {
            if ((reader != null))
            {
                reader.Dispose();
            }
            persistence.CompleteCommand();
        }
        return ret;
    }
1

There are 1 best solutions below

2
rAhulD On

I am assuming you already have created the DbContext, Models and Migrations. If not, I recommend to read through the Entity Framework Core MS docs (I am assuming you are on dot net core)

Here is the page you can begin with.

You have to create the DbContext derivation which will include all your DbSet (data models/DB entities). I am assuming it be looking as below, if you already have,

public class User
{
    //Your User table columns named properties
}

public class MyDbContext : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");
    }
}

Here's the EF equivalent of your code mentioned above,

public static User[] GetSpaces(System.Guid userId)
{
    if (userId.Equals(Guid.Empty))
    {
        throw new System.ArgumentNullException("userId");
    }
    var ret = default(User[]);
    try
    {
        using (var persistence = new MyDbContext())
        {
            ret = persistence.Users.FromSql("GetSpaces", userId).ToArrayAsync().Result;
        }
    }
    catch { }
    finally { }
    return ret;
}

You will really have to provide much details to make this solution matching your requirement, but this is much of how it would translate to what your ask is.