1

I have tried various ways to get my application to work and have tried searching here for a solution to the various messages that I received but none have worked. So, I decided to start from scratch and ask for the proper way to connect my 2 applications.

I have a Solution with 2 projects inside it:

  • Application
    Application is a stand alone app that has login/logout, user management capabilities etc.
    Application is connected to a database

  • ApplicationApi
    ApplicationApi is a console application that references Application
    ApplicationApi's main task is to receive any incoming requests via its ApiController and then pass that to Application who will then return a result to it
    ApplicationApi is not connected to a database

This is the function in ApplicationApi that I want to call a function in Application

namespace ApplicationApi.Controllers
{
    public class TokenController:ApiController
    {
        public String Get()
        {
            var auth = new AuthenticationController();
            return auth.AuthenticateUser("user@one.com", "P@ssw0rd").ToString();
        }
    }
}

This is the function that is called by ApplicationApi

namespace Application.Controllers.API
{
    public class AuthenticationController : Controller
    {
        ApplicationDbContext dbContext = new ApplicationDbContext();
        Logger log = LogManager.GetCurrentClassLogger();
        PasswordHasher passwordHasher = new PasswordHasher();
        public bool AuthenticateUser(String username, String password)
        {
            try
            {
                var user = dbContext.Users.FirstOrDefault(u => u.UserName == username);
                if (user == null)
                {
                    log.Error(username + " not found");
                    return false;
                }
                else
                {
                    var result = passwordHasher.VerifyHashedPassword(user.PasswordHash, password);
                    if (result == PasswordVerificationResult.Success)
                    {
                        return true;
                    }
                    else
                    {
                        log.Error("Invalid password for user: " + username);
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                log.Error(e, "Exception found for user: " + username);
                return false;
            }
        }
    }
}

At the moment, when I make a request from Postman, I get this error from Application when debug is on:

{"No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information."}

This looks like its saying that my ApplicationApi needs to have EntityFramework or needs to refer to it to work?

That is where I get confused. ApplicationApi is only supposed to ask Application for a result. Why does this error happen? ApplicationApi does not have any database connections. Nor does it need to to. Its only a transport layer.

Maybe I've started wrong so I hope someone can guide me on how to actually do this.

EDIT: My question is not a duplicate of that one because I'm asking why I need EntityFramework in an Api Project that won't connect to a database?

JianYA
  • 2,750
  • 8
  • 60
  • 136
  • Have you installed EntityFramework Nuget Package in your API project? – Hooman Bahreini Aug 02 '18 at 03:46
  • Possible duplicate of [No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'](https://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name) – Hooman Bahreini Aug 02 '18 at 03:47
  • @Hooman why would I need EntityFramework for my API project? It doesn't connect to a db. Only Application does that and there is already EntityFramework installed there. – JianYA Aug 02 '18 at 03:56
  • Aren't you making this call in your API project? dbContext.Users.FirstOrDefault – Hooman Bahreini Aug 02 '18 at 03:59
  • No, Its called only in the Application project. API is the other one that is just calling AuthenticateUsers – JianYA Aug 02 '18 at 04:01
  • maybe this can help https://stackoverflow.com/a/22970805/3111542 – Den Aug 02 '18 at 04:06
  • I get this error now when I try your solution @Den {"Database 'C:\\Users\\ajian\\Source\\Repos\\Application\\Application\\App_Data\\aspnet-Application-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf' already exists. Choose a different database name.\r\nCannot attach the file 'C:\\Users\\ajian\\Source\\Repos\\Application\\ApplicationApi\\bin\\Debug\\aspnet-Application-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c.mdf' as database 'aspnet-Application-5cb5b1db-6c48-49c7-93b2-ba81ded39c1c'."} – JianYA Aug 02 '18 at 04:37
  • @Den your solution requires me to remove the connection string from my Application. However, Application is a stand alone app as well. It needs the connection string – JianYA Aug 02 '18 at 04:38
  • If it is a standalone app why do you use it as a class library? – Den Aug 02 '18 at 04:45
  • I dont use it as a class library? I want to call a function from Application that is written in the API folder. I'm confused. Is what I want to do possible? – JianYA Aug 02 '18 at 04:50
  • Have you installed Entity Framework in your Application Project? – Hooman Bahreini Aug 02 '18 at 05:22
  • Yes, I have also referenced EntityFramework.SQLServer.dll in my ApplicationApi – JianYA Aug 02 '18 at 05:32
  • I can send the zipped file over to you if you want to take a look at it. Its a small project and I want to learn and see if this is possible. – JianYA Aug 02 '18 at 05:42

0 Answers0