2

Ok, hope to not get too many flags, but it's to annoying.

I have a method in my controller which calls a method from another class:

offerForCreate.Rating = CalculateRating.CreateRating(addOffer);

and entire called class :

public class CalculateRating
{
    private readonly DataContext mainContext;

    public CalculateRating(DataContext mainContext)
    {
        this.mainContext = mainContext;
    }

    // calcul rating oferte noi
    public decimal CreateRating(OfferForCreate offer)
    {
        decimal rating = mainContext.Database.FromSql<decimal>("RatingCalculator", offer.locationId, offer.typeId);

        return rating;
    }
}

I get an error when try to execute this procedure:

Error CS1061: 'DatabaseFacade' does not contain a definition for 'FromSql' and no extension method 'FromSql' accepting a first argument of type 'DatabaseFacade' could be found

and another if I don't create an instance of CalculateRating class in my controller :

Controllers\AnnouncesController.cs(127,37): error CS0120: An object reference is required for the non-static field, method, or property 'CalculateRating.CreateRating(OfferForCreate)

Everywhere I see must specify the entity, but what entity I can specify if my stored procedure use multiple tables?

Asp.Net Core Web API

1 Answer 1

2

You can execute stored proc like this:

 using (var command = mainContext.Database.GetDbConnection().CreateCommand())
   {
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.CommandText = "dbo.RatingCalculator";

       var locationIdParam = new System.Data.SqlClient.SqlParameter("@locationId", System.Data.SqlDbType.Int);
       locationIdParam .Value = offer.locationId;

        //DO same for typeId parameter

          //Params to Parameters collection
          command.Parameters.Add(locationIdParam);

       command.Connection.Open();
       return (double)command.ExecuteScalar();
   }

Controllers\AnnouncesController.cs(127,37): error CS0120: An object reference is required for the non-static field, method, or property 'CalculateRating.CreateRating(OfferForCreate)

This error is occuring because if you declare CalculateRating as static you can not reference in non-static field mainContext.

You should create an instance of your CalculateRating class using Dependency Injection. Here are steps:

  1. Create an interface ICalculateRating

    public interface ICalculateRating { decimal CreateRating(OfferForCreate offer); }

  2. Update CalculateRating class to implement ICalculateRating

  3. Register the DBContext and ICalculateRating mappings in ConfigureServices method of Startup.cs file like this:

    services.AddDbContext<DbContext>(opts=> { opts.UseSqlServer("sqlserver conntection string") }, ServiceLifetime.Scoped);

    services.AddTransient<ICalculateRating, CalculateRating>();

  4. In your controller constructor, input an argument of type ICalculateRating which will be injected by Microsoft Dependency Injection framework at runtime:

    private readonly ICalculateRating _calculateRating; public MyController(ICalculateRating calculateRating) { _calculateRating = calculateRating; }

You can then call the method like this: offerForCreate.Rating = _calculateRating.CreateRating(addOffer);

Sign up to request clarification or add additional context in comments.

1 Comment

awesome. thanks a lot. I never tought that is necesary to create interface and inject it in my controller. and about execution of the procedure, it's clean code and easy to understand. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.