首页 > 解决方案 > 对数据库的全局访问不起作用

问题描述

我在这里进行了 WebAPI 和 Angular 的协作 - https://angular.io/tutorial 但他们自己在那里填补了英雄。我需要从我已经创建的数据库中获取名称。WebAPI 中的请求有效。当我通过 webapi 项目去 api / 剧院时,我输出正确。

所以我需要你的建议。那么,我是如何让全球数据库无法使用的呢?

如果我在 theatre.service 文件中执行以下操作 -

     export class TheatresService {
  private theatreUrl = 'api/Theatre';

那么什么都行不通

如果我这样说——

     export class TheatresService {
  private theatreUrl = 'api/theatres';

我扔在文件中,它将正确显示。

带有剧院 in-memory-data-service.ts 的文件

import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Theatre } from './theatres.model';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class InMemoryDataServiceTheatre implements InMemoryDbService {
  createDb() {
    const theatres = [
      { id: 11, name: 'theatre' },
      { id: 12, name: 'opera'},
      { id: 13, name: 'operatta' },
    ];
    return {theatres};
  }
}

和 app.module.ts

    @NgModule({
  declarations: [
   .......
  ],
  imports: [
    ........
    HttpClientModule,
    HttpClientInMemoryWebApiModule.forRoot(
      InMemoryDataServiceTheatre, {dataEncapsulation: false}
    )
  ],
  providers: [TheatresService],
  bootstrap: [AppComponent]
})
export class AppModule { }

告诉我有什么问题。或删除显示如何使用内部数据库的链接

更新我的模型

     namespace TicketSalePoint.Data.Models
{
    [Table("Theatres")]
    public class Theatre
    {
        public Theatre() { }
        public int Id { get; set; }
        public string Login { get; set; }
        public string Password { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public DateTime RegistrationDate { get; set; }

        public virtual ICollection<Show> Shows { get; set; }
    }
}

我的带有 webAPI 的控制器

    namespace TicketSalePointWebAPI.Controllers
{
    [AllowAnonymous]
    public class TheatreController : ApiController
    {
        [HttpGet]
        public async Task<IHttpActionResult> GetNames()
        {
            using (var dataBaseContext = new DataBaseContext())
            {
                dataBaseContext.Database.Connection.Open();
                var query = from theatre in dataBaseContext.Theatres
                            select new { theatre.Id, theatre.Name };

                return Ok(await query.ToListAsync());
            }
        }
    }
}

我看到错误:

{body: {…}, url: "api/Theatre", headers: HttpHeaders, status: 404, statusText: "Not Found"}
body: {error: "Collection 'Theatre' not found"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
status: 404
statusText: "Not Found"
url: "api/Theatre"
__proto__: Object

标签: angularasp.net-web-apiasp.net-web-api2asp.net-web-api-routing

解决方案


推荐阅读