我想建立一個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」解析服務。
謝謝你的作品 –