Browse Source

Add ef sent message demo to SqlServer samples

master
Savorboard 6 years ago
parent
commit
d753b1a2d6
6 changed files with 167 additions and 4 deletions
  1. +23
    -0
      samples/Sample.Kafka.SqlServer/AppDbContext.cs
  2. +37
    -1
      samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs
  3. +38
    -0
      samples/Sample.Kafka.SqlServer/Migrations/20180825123925_init.Designer.cs
  4. +30
    -0
      samples/Sample.Kafka.SqlServer/Migrations/20180825123925_init.cs
  5. +36
    -0
      samples/Sample.Kafka.SqlServer/Migrations/AppDbContextModelSnapshot.cs
  6. +3
    -3
      samples/Sample.Kafka.SqlServer/Startup.cs

+ 23
- 0
samples/Sample.Kafka.SqlServer/AppDbContext.cs View File

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

+ 37
- 1
samples/Sample.Kafka.SqlServer/Controllers/ValuesController.cs View File

@@ -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)


+ 38
- 0
samples/Sample.Kafka.SqlServer/Migrations/20180825123925_init.Designer.cs View File

@@ -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
}
}
}

+ 30
- 0
samples/Sample.Kafka.SqlServer/Migrations/20180825123925_init.cs View File

@@ -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");
}
}
}

+ 36
- 0
samples/Sample.Kafka.SqlServer/Migrations/AppDbContextModelSnapshot.cs View File

@@ -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
}
}
}

+ 3
- 3
samples/Sample.Kafka.SqlServer/Startup.cs View File

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


Loading…
Cancel
Save