Last active
June 22, 2019 21:16
-
-
Save gistlyn/2c3e17c98d20d7073be5f24cb16ab02e to your computer and use it in GitHub Desktop.
Configure AppHost to use OrmLite with MySql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dotnet add package ServiceStack.OrmLite.MySql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using ServiceStack; | |
| using ServiceStack.Data; | |
| using ServiceStack.DataAnnotations; | |
| using ServiceStack.OrmLite; | |
| namespace MyApp | |
| { | |
| public class MyTable | |
| { | |
| [AutoIncrement] | |
| public int Id { get; set; } | |
| public string Name { get; set; } | |
| } | |
| public class ConfigureDb : IConfigureServices, IConfigureAppHost | |
| { | |
| IConfiguration Configuration { get; } | |
| public ConfigureDb(IConfiguration configuration) => Configuration = configuration; | |
| public void Configure(IServiceCollection services) | |
| { | |
| services.AddSingleton<IDbConnectionFactory>(new OrmLiteConnectionFactory( | |
| Configuration.GetConnectionString("DefaultConnection") | |
| ?? "Server=localhost;User Id=test;Password=test;Database=test;Pooling=true;MinPoolSize=0;MaxPoolSize=200", | |
| MySqlDialect.Provider)); | |
| } | |
| public void Configure(IAppHost appHost) | |
| { | |
| appHost.GetPlugin<SharpPagesFeature>()?.ScriptMethods.Add(new DbScriptsAsync()); | |
| using (var db = appHost.Resolve<IDbConnectionFactory>().Open()) | |
| { | |
| if (db.CreateTableIfNotExists<MyTable>()) | |
| { | |
| db.Insert(new MyTable { Name = "Seed Data for new MyTable" }); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment