0
votes

I am trying to use existing data layer with a web api project it was working before i added the authorisation attribute however now its saying the table is not part of the context I can only persume this is because the username and password store uses a local sql db and not sql server that my entity belong to.

Now i have got the token from a user a setup under the register function which puts it in the local db. So that gave me back the following which i then place in the Authorization header of post man to test the api.

GET /api/CustomerInfo/B737069E-A835-40EC-BDF7-0CA4821C4D59 HTTP/1.1 Host: localhost:54010 Content-Type: application/json Authorization: bearer s159EgZ1KwRvq53eEWeAYTupJlpUGLaDWXTy4Z39ezUX3Ac41HA7v6l3F2uFfW8eJde8eSS-kWKTn05IIkHnIKS-U2IdoaRssS8hZhCAqe9REL8SWFqo92i9kZrbnSMIhC1V73Lt0kMYf_mnjSSFM33OGVe7JHR4-5NUezvO--eI9XtFx6OH5ZCYYmBZo2UhIxt3Ma7jDaFv22c5lx7wrCfL1d2xXgrWTGtWL7QM6mZ18nxncsOT49XFpcEQhAv0a1DRf9ygVZUVYq-L0JSlz_PnZy6YUPuzlzmL0lgcjhTXKYK4f7chI3cIG9Tl96nfn9VxFMQm8XDgxHBWHNFsHp7z3JG9FwGIAtirfiizt-SAmlcXGxxZ0BONHGS18wlMjq_1DkmtGEoz9LQoqn6AdmydGO2fXH2Of-jNR_VCktGvE9D5Uj9PmysU7FNtNtUcoIlYgoTlgPlUnl9k443C1uBU3ZoZCh10Z2wRDoceGMtjFycOW99N00l4fOXGhjmn Cache-Control: no-cache Postman-Token: 1fbb870c-af27-aeff-7d36-7520b530c6b8

This is how i have setup barrer token

public class CustomerInfoController : ApiController
{
    private portalEntities db = new portalEntities();

    // GET: api/CustomerInfo


    public IQueryable<tblPortalCustomerInfo> GettblPortalCustomerInfoes()
    {
        return db.tblPortalCustomerInfoes;
    }

    // GET: api/CustomerInfo/5

      [Authorize]
    public async Task<IHttpActionResult> GettblPortalCustomerInfo(Guid id)
    {
        tblPortalCustomerInfo tblPortalCustomerInfo = await db.tblPortalCustomerInfoes.FindAsync(id);
        if (tblPortalCustomerInfo == null)
        {
            return NotFound();
        }

        return Ok(tblPortalCustomerInfo);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
    [Authorize]
    private bool tblPortalCustomerInfoExists(Guid id)
    {
        return db.tblPortalCustomerInfoes.Count(e => e.id == id) > 0;
    }
}

This is the customer info class i am calling

public class CustomerInfoController : ApiController
{
    private portalEntities db = new portalEntities();

    // GET: api/CustomerInfo


    public IQueryable<tblPortalCustomerInfo> GettblPortalCustomerInfoes()
    {
        return db.tblPortalCustomerInfoes;
    }

    // GET: api/CustomerInfo/5

      [Authorize]
    public async Task<IHttpActionResult> GettblPortalCustomerInfo(Guid id)
    {
        tblPortalCustomerInfo tblPortalCustomerInfo = await db.tblPortalCustomerInfoes.FindAsync(id);
        if (tblPortalCustomerInfo == null)
        {
            return NotFound();
        }

        return Ok(tblPortalCustomerInfo);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
    [Authorize]
    private bool tblPortalCustomerInfoExists(Guid id)
    {
        return db.tblPortalCustomerInfoes.Count(e => e.id == id) > 0;
    }
}

Error in post man is https://snag.gy/kQYIUP.jpg

"ExceptionMessage": "The entity type tblPortalCustomerInfo is not part of the model for the current context.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System.Data.Entity.Internal.Linq.InternalSet1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)\r\n at System.Data.Entity.DbSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)\r\n at System.Data.Entity.DbSet1.FindAsync(Object[] keyValues)\r\n at portalWebApi.Controllers.CustomerInfoController.<GettblPortalCustomerInfo>d__2.MoveNext() in C:\\Projects\\Client-Portal\\portalWebApi\\Controllers\\CustomerInfoController.cs:line 35\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<CastToObject>d__31.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

1
Are you using asp.net Identity? do you use separate context for both backend and security tables?Haitham Shaddad
@HaithamShaddad the web api 2 template usings identy and the sep context to my own context but waht i dont understand is why it cant find my tables when i use the authorise taguser7360853

1 Answers

0
votes

You have two options to solve this problem:

  1. Move the relevant tables from the local DB to the SQL Server and update the existing EF model.

  2. Create a new EF model for the local DB and use the new context instead of the existing portalEntities context for authenticating users.