Files
sql-server-samples/samples/features/json/Dapper-Orm/Startup.cs
T
Jovan Popovic b43e4c82d5 Added Dapper JSON Type Handler example
In json/Dapper-ORM is added new example that maps string[] and object properties to JSON.
2017-10-05 20:42:26 +02:00

51 lines
1.9 KiB
C#

using Dapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Data;
using System.Data.SqlClient;
namespace ProductCatalog
{
public class Startup
{
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)
.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)
{
// Map sting[] and object properties in model to JSON column in database.
SqlMapper.AddTypeHandler(typeof(string[]), new StringArrayJsonMapper());
SqlMapper.AddTypeHandler(typeof(object), new ObjectJsonMapper());
string ConnString = Configuration["ConnectionStrings:ProductCatalog"];
services.AddTransient<IDbConnection>(_ => new SqlConnection(ConnString));
// Add framework services.
services.AddMvc();
}
// 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.UseStaticFiles();
app.UseMvc();
}
}
}