2017-02-22 6 views
1

エンティティ "ユーザー"、DbContext、リポジトリ、コントローラを作成して動作するかどうかをテストするために.netコアを使用してAPIを構築しようとしていますが、 internelサーバーエラーを示す常に500 Http statu。データベースに接続します.netコア

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

とリポジトリここ

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 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です: はここに私のエンティティでありますようやくここに私のCo ntroller:「System.InvalidOperationExceptionが」、タイプのサービスを解決できません「SocilNetworkApi.Models.UserRepository」:実際には

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

    } 

それは、診断ツールに例外を示します。

答えて

1

問題はEFに関連するが、それは、コントローラの依存

コントローラは、そのタイプを解決するためにUserRepositoryので、ランタイムの試行に応じているが、それがマップされていないのですされていません。それがあなたにそのエラーを与える理由です。

あなたはこれがあなたの問題を解決する必要がありますUserRepository

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

IUserRepository上のコントローラが依存にする必要はありません。

+0

ありがとうございます –

0

"SocilNetworkApi.Models.UserRepository"は、それがUserControllerコンストラクタに注入されるために依存性注入コンテナに登録する必要があります。

+0

しかし、それはalredyを行った –

+0

申し訳ありませんが、私はそれを逃した、私はモバイルです。具体的なクラスではなくインターフェイスを注入する必要があります。 –

関連する問題