Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-First Entity Framework w/ Stored Procedure returning results from complex Full-text Searches

I am looking for design advice for the following scenario:

I have a code-first EF5 MVC application. I am building a full-text search function which will incorporate multiple weighted columns from many tables. As I cannot create view with an index from these tables (some of them contain text / binary columns), I have created a stored procedure which will output the ID of my object (eg. PersonID) and the rank associated with that object based on the search terms.

My current approach is to create a helper class for executing full text searches which call the stored procedure(s) and load all the objects from the context based on the returned IDs.

My questions are:

  1. Does my approach seem sensible / follow reasonable best practice?
  2. Has anyone else done something similar with any lessons learned?
  3. Is there a way to do this more efficiently (i.e. have the results of the stored procedure return/map to the entities directly without an additional look-up required?)

UPDATE

Moved my detailed implementation from an edit of the question into its own answer to be more in line with what is recommended frequently @ meta.stackexchange.com

like image 462
Matthew Avatar asked Jan 09 '13 18:01

Matthew


People also ask

How do you call a stored procedure using code first in Entity Framework?

To use a Stored Procedure with the Code First model, we need to override the OnModelCreating method of DBContext and add the following code to map the Stored Procedure. The MapToStoreProcedures method has two overloaded methods, one method is without a parameter.

Can we create stored procedure in code first approach?

Entity Framework 6 Code-First provides the ability to create and use a stored procedure for add, update, and delete operations when the SaveChanges() method is called. Let's use stored procedures for the CUD (Create, Update, Delete) operations for the following Student entity.

How do you call a stored procedure in Entity Framework 6 first?

Here first you need to specify the Function Import Name which is the name of the function used to call the Stored Procedure and then select the Stored Procedure that will be executed when the function is called. The Return Type is selected as Entities which is the Customer Entity class.


1 Answers

  1. Seeing as you can't use SQL methods like containstable with entityframework code first which the rest of your application could be using you could be 'forced' to do something with a storedprocedure like your describe. Whether it's best practice I don't know. However it it gets the job done I don't see why it wouldn't be sensible.
  2. Yes - I have and still am working on a project build around EF codefirst where I had to do a fairly complex search that included several search parameters marked as 'must have' and several values marked as 'nice to have' and in from that return a weighted result.
  3. Depending on the complexity of the result set I don't think you need to do a second roundtrip to the database and I will show you a way I have been doing it below.

Bear in mind that below is simply an example:

    public List<Person> GetPeople(params string[] p)
    {
        var people = new List<Person>();

        using (var db = new DataContext())
        {
            var context = ((IObjectContextAdapter)db).ObjectContext;

            db.Database.Connection.Open();

            var command = db.Database.Connection.CreateCommand();
            command.CommandText = "SomeStoredProcedureReturningWeightedResultSetOfPeople";
            command.CommandType = System.Data.CommandType.StoredProcedure;

            //Add parameters to command object

            people = context.Translate<Person>(command.ExecuteReader()).ToList();
        }

        return people;
    }

Even though the storedprocedure will have a column for the weight value it won't get mapped when you translate it. You could potentially derive a class from Person that includes the weight value if you needed it.

like image 179
Drauka Avatar answered Sep 22 '22 23:09

Drauka