@@ -0,0 +1,23 @@ | |||
using Microsoft.EntityFrameworkCore; | |||
namespace Sample.Kafka.SqlServer | |||
{ | |||
public class Person | |||
{ | |||
public int Id { get; set; } | |||
public string Name { get; set; } | |||
} | |||
public class AppDbContext : DbContext | |||
{ | |||
public const string ConnectionString = "Server=localhost;Integrated Security=SSPI;Database=testcap"; | |||
public DbSet<Person> Persons { get; set; } | |||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |||
{ | |||
optionsBuilder.UseSqlServer(ConnectionString); | |||
} | |||
} | |||
} |
@@ -27,7 +27,7 @@ namespace Sample.Kafka.SqlServer.Controllers | |||
[Route("~/adonet/transaction")] | |||
public IActionResult AdonetWithTransaction() | |||
{ | |||
using (var connection = new SqlConnection(Startup.ConnectionString)) | |||
using (var connection = new SqlConnection(AppDbContext.ConnectionString)) | |||
{ | |||
using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false)) | |||
{ | |||
@@ -43,6 +43,42 @@ namespace Sample.Kafka.SqlServer.Controllers | |||
return Ok(); | |||
} | |||
[Route("~/adonet/autocommit/transaction")] | |||
public IActionResult AdonetAutoCommitWithTransaction() | |||
{ | |||
using (var connection = new SqlConnection(AppDbContext.ConnectionString)) | |||
{ | |||
using (var transaction = connection.BeginTransaction(_capBus, autoCommit: true)) | |||
{ | |||
//your business code | |||
connection.Execute("insert into dbo.test1(tname) values('test');", transaction: transaction); | |||
_capBus.Publish("sample.kafka.sqlserver", DateTime.Now); | |||
} | |||
} | |||
return Ok(); | |||
} | |||
[Route("~/ef/transaction")] | |||
public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext) | |||
{ | |||
using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false)) | |||
{ | |||
dbContext.Persons.Add(new Person() { Name = "ef.transaction" }); | |||
for (int i = 0; i < 2; i++) | |||
{ | |||
_capBus.Publish("sample.kafka.sqlserver", DateTime.Now); | |||
} | |||
dbContext.SaveChanges(); | |||
trans.Commit(); | |||
} | |||
return Ok(); | |||
} | |||
[NonAction] | |||
[CapSubscribe("sample.kafka.sqlserver")] | |||
public void Subscriber(DateTime time) | |||
@@ -0,0 +1,38 @@ | |||
// <auto-generated /> | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.EntityFrameworkCore.Infrastructure; | |||
using Microsoft.EntityFrameworkCore.Metadata; | |||
using Microsoft.EntityFrameworkCore.Migrations; | |||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |||
using Sample.Kafka.SqlServer; | |||
namespace Sample.Kafka.SqlServer.Migrations | |||
{ | |||
[DbContext(typeof(AppDbContext))] | |||
[Migration("20180825123925_init")] | |||
partial class init | |||
{ | |||
protected override void BuildTargetModel(ModelBuilder modelBuilder) | |||
{ | |||
#pragma warning disable 612, 618 | |||
modelBuilder | |||
.HasAnnotation("ProductVersion", "2.1.1-rtm-30846") | |||
.HasAnnotation("Relational:MaxIdentifierLength", 128) | |||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||
modelBuilder.Entity("Sample.Kafka.SqlServer.Person", b => | |||
{ | |||
b.Property<int>("Id") | |||
.ValueGeneratedOnAdd() | |||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||
b.Property<string>("Name"); | |||
b.HasKey("Id"); | |||
b.ToTable("Persons"); | |||
}); | |||
#pragma warning restore 612, 618 | |||
} | |||
} | |||
} |
@@ -0,0 +1,30 @@ | |||
using Microsoft.EntityFrameworkCore.Metadata; | |||
using Microsoft.EntityFrameworkCore.Migrations; | |||
namespace Sample.Kafka.SqlServer.Migrations | |||
{ | |||
public partial class init : Migration | |||
{ | |||
protected override void Up(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.CreateTable( | |||
name: "Persons", | |||
columns: table => new | |||
{ | |||
Id = table.Column<int>(nullable: false) | |||
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), | |||
Name = table.Column<string>(nullable: true) | |||
}, | |||
constraints: table => | |||
{ | |||
table.PrimaryKey("PK_Persons", x => x.Id); | |||
}); | |||
} | |||
protected override void Down(MigrationBuilder migrationBuilder) | |||
{ | |||
migrationBuilder.DropTable( | |||
name: "Persons"); | |||
} | |||
} | |||
} |
@@ -0,0 +1,36 @@ | |||
// <auto-generated /> | |||
using Microsoft.EntityFrameworkCore; | |||
using Microsoft.EntityFrameworkCore.Infrastructure; | |||
using Microsoft.EntityFrameworkCore.Metadata; | |||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | |||
using Sample.Kafka.SqlServer; | |||
namespace Sample.Kafka.SqlServer.Migrations | |||
{ | |||
[DbContext(typeof(AppDbContext))] | |||
partial class AppDbContextModelSnapshot : ModelSnapshot | |||
{ | |||
protected override void BuildModel(ModelBuilder modelBuilder) | |||
{ | |||
#pragma warning disable 612, 618 | |||
modelBuilder | |||
.HasAnnotation("ProductVersion", "2.1.1-rtm-30846") | |||
.HasAnnotation("Relational:MaxIdentifierLength", 128) | |||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||
modelBuilder.Entity("Sample.Kafka.SqlServer.Person", b => | |||
{ | |||
b.Property<int>("Id") | |||
.ValueGeneratedOnAdd() | |||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); | |||
b.Property<string>("Name"); | |||
b.HasKey("Id"); | |||
b.ToTable("Persons"); | |||
}); | |||
#pragma warning restore 612, 618 | |||
} | |||
} | |||
} |
@@ -5,13 +5,13 @@ namespace Sample.Kafka.SqlServer | |||
{ | |||
public class Startup | |||
{ | |||
public const string ConnectionString = "Server=localhost;Integrated Security=SSPI;Database=testcap"; | |||
public void ConfigureServices(IServiceCollection services) | |||
{ | |||
services.AddDbContext<AppDbContext>(); | |||
services.AddCap(x => | |||
{ | |||
x.UseSqlServer(ConnectionString); | |||
x.UseEntityFramework<AppDbContext>(); | |||
x.UseKafka("localhost:9092"); | |||
x.UseDashboard(); | |||
}); | |||