2017-02-22 124 views
1

我想建立一個API使用.NET核心,我創建實體「用戶」,DbContext,知識庫和控制器來測試它是否工作,但是當我測試與郵差我收到總是500個Http statu指示內部服務器錯誤。 這裏是我的實體:連接到數據庫.net核心

public class User 


     { 
      [Key] 
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
      public long Id { get; set; } 
      [Required] 
      public string UserName { get; set; } 
      public string Email { get; set; } 
      [Required] 
      public string Password { get; set; } 
      public ICollection<Post> Posts { get; set; } = new List<Post>(); 
     } 

這裏是日的DbContext:

public class NetworkDbContext : DbContext 
    { 
     public NetworkDbContext(DbContextOptions options) : base(options) 
     { 
      Database.EnsureCreated(); 
     } 
     public DbSet<User> Users { get; set; } 
     public DbSet<Post> Posts { get; set; } 
     public DbSet<Comment> Comments { get; set; } 
    } 

這裏是庫:

public class UserRepository : IUserRepository 
    { 
     private readonly NetworkDbContext _context; 
     public UserRepository(NetworkDbContext context) 
     { 
      _context = context; 
     } 
     public void AddUser(User user) 
     { 
      _context.Users.Add(user); 
      _context.SaveChanges(); 
     } 

     public IEnumerable<User> GetAllUsers() 
     { 
      return _context.Users.ToList(); 
     } 

     public void RemoveUser(User user) 
     { 
      _context.Users.Remove(user); 
      _context.SaveChanges(); 
     } 

     public void Update(User user) 
     { 
      _context.Users.Update(user); 
      _context.SaveChanges(); 
     } 
    } 

的startup.cs:

public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 

      if (env.IsEnvironment("Development")) 
      { 
       // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. 
       builder.AddApplicationInsightsSettings(developerMode: true); 
      } 

      builder.AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     // This method gets called by the runtime. Use this method to add services to the container 
     public void ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      services.AddApplicationInsightsTelemetry(Configuration); 

      services.AddMvc(); 
      var connectionString = Configuration["connectionString"]; //connection string to sqlServer 
      services.AddDbContext<NetworkDbContext>(o => o.UseSqlServer(connectionString)); 
      services.AddScoped<IUserRepository, UserRepository>(); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      app.UseApplicationInsightsRequestTelemetry(); 

      app.UseApplicationInsightsExceptionTelemetry(); 

      app.UseMvc(); 
     } 
    } 

和終於,這是我的合作伙伴ntroller:

[Route("api/user")] 
    public class UserController : Controller 
    { 
     private UserRepository _repository { get; set; } 
     public UserController(UserRepository repository) 
     { 
      _repository = repository; 
     } 
     [HttpPost] 
     public void AddUser([FromBody] User user) 
     { 
      if (user == null) 
     { 
      return BadRequest(); 
     } 
     _repository.AddUser(user); 
     return CreatedAtRoute("GetUser", user); 
     } 

    } 

事實上,它在診斷工具指示異常:「System.InvalidOperationException」,無法爲類型「SocilNetworkApi.Models.UserRepository」解析服務。

回答

1

的問題是不相關的EF但它的控制器

控制器取決於UserRepository所以運行時嘗試解決該類型的依賴,但是,這不是映射。這就是爲什麼它給你這個錯誤。

你需要讓你的控制器依賴於IUserRepository不是UserRepository

[Route("api/user")] 
public class UserController : Controller 
{ 
    private IUserRepository _repository { get; set; } 
    public UserController(IUserRepository repository) 
    { 
     _repository = repository; 
    } 
} 

這應該解決您的問題。

+0

謝謝你的作品 –

0

「SocilNetworkApi.Models.UserRepository」需要註冊到依賴注入容器中,以便將其注入到UserController構造函數中。

+0

但它已完成 –

+0

對不起,我錯過了,我在手機上。你需要注入接口而不是具體的類。 –