【问题标题】:Table is not in the current context web api issue表不在当前上下文 web api 问题中
【发布时间】:2016-12-31 15:07:57
【问题描述】:

我正在尝试将现有数据层与在我添加授权属性之前正在工作的 web api 项目一起使用,但是现在它说该表不是上下文的一部分我只能认为这是因为用户名和密码存储使用本地 sql db,而不是我的实体所属的 sql server。

现在我从用户那里获得了令牌,并在注册功能下进行了设置,将其放入本地数据库。所以这给了我以下内容,然后我将其放在 post man 的 Authorization 标题中以测试 api。

GET /api/CustomerInfo/B737069E-A835-40EC-BDF7-0CA4821C4D59 HTTP/1.1 主机:本地主机:54010 内容类型:application/json 授权: 持票人 s159EgZ1KwRvq53eEWeAYTupJlpUGLaDWXTy4Z39ezUX3Ac41HA7v6l3F2uFfW8eJde8eSS-kWKTn05IIkHnIKS-U2IdoaRssS8hZhCAqe9REL8SWFqo92i9kZrbnSMIhC1V73Lt0kMYf_mnjSSFM33OGVe7JHR4-5NUezvO - eI9XtFx6OH5ZCYYmBZo2UhIxt3Ma7jDaFv22c5lx7wrCfL1d2xXgrWTGtWL7QM6mZ18nxncsOT49XFpcEQhAv0a1DRf9ygVZUVYq-L0JSlz_PnZy6YUPuzlzmL0lgcjhTXKYK4f7chI3cIG9Tl96nfn9VxFMQm8XDgxHBWHNFsHp7z3JG9FwGIAtirfiizt-SAmlcXGxxZ0BONHGS18wlMjq_1DkmtGEoz9LQoqn6AdmydGO2fXH2Of-jNR_VCktGvE9D5Uj9PmysU7FNtNtUcoIlYgoTlgPlUnl9k443C1uBU3ZoZCh10Z2wRDoceGMtjFycOW99N00l4fOXGhjmn 缓存控制:无缓存邮递员令牌: 1fbb870c-af27-aeff-7d36-7520b530c6b8

这就是我设置 barrer 令牌的方式

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;
    }
}

这是我正在调用的客户信息类

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;
    }
}

邮递员的错误是 https://snag.gy/kQYIUP.jpg

"ExceptionMessage": "实体类型 tblPortalCustomerInfo 不是 当前上下文的模型的一部分。", "ExceptionType": “System.InvalidOperationException”,“StackTrace”:“在 System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(类型 entityType)\r\n 在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型 entityType)\r\n 在 System.Data.Entity.Internal.Linq.InternalSet1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()\r\n 在 System.Data.Entity.Internal.Linq.InternalSet1.FindAsync(CancellationToken cancellationToken, Object[] keyValues)\r\n at System.Data.Entity.DbSet1.FindAsync(CancellationToken cancelToken, 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--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- 从先前抛出异常的位置结束堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

【问题讨论】:

  • 你在使用 asp.net 身份吗?您是否对后端表和安全表使用单独的上下文?
  • @HaithamShaddad web api 2 模板使用 identy 和 sep 上下文到我自己的上下文,但我不明白为什么当我使用授权标签时它无法找到我的表

标签: c# entity-framework asp.net-web-api2


【解决方案1】:

你有两个选择来解决这个问题:

  1. 将相关表从本地 DB 移动到 SQL Server 并更新现有的EF 模型。

  2. 为本地 DB 创建一个新的 EF 模型,并使用新的上下文而不是现有的 portalEntities 上下文来验证用户。

【讨论】:

  • 但是 surley 我在那里有我的上下文,这可以在没有授权标签的情况下正常发送数据,但是一旦我添加了承载代码,它就不会如何整合表格。
  • 你能帮忙看看怎么做吗
猜你喜欢
  • 2022-01-02
  • 2021-03-05
  • 2020-07-31
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多