Skip to content

Commit e9a88b3

Browse files
committed
EfCore and MongoDb test CoreMVC sample
1 parent aab57db commit e9a88b3

26 files changed

Lines changed: 267 additions & 51 deletions

SharpRepository.EfCoreRepository/EfCoreConfigRepositoryFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected DbContext GetDbContext()
6565
if (!String.IsNullOrEmpty(tmpDbContextType))
6666
{
6767
dbContextType = Type.GetType(tmpDbContextType);
68+
69+
if (dbContextType == null)
70+
{
71+
throw new NotImplementedException("Unable to find " + tmpDbContextType + " class");
72+
}
6873
}
6974

7075
// TODO: look at dbContextType (from Enyim.Caching configuration bits) and how it caches, see about implementing cache or expanding FastActivator to take parameters

SharpRepository.Ioc.Microsoft.DependencyInjection/ServicesCollectionExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ public static IServiceProvider UseSharpRepository(this IServiceCollection servic
1717
throw new ConfigurationErrorsException("Configuration section not found.");
1818

1919
var sharpRepoConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(configurationSection);
20-
20+
21+
return services.UseSharpRepository(sharpRepoConfig, repositoryName);
22+
}
23+
24+
public static IServiceProvider UseSharpRepository(this IServiceCollection services, ISharpRepositoryConfiguration sharpRepoConfig, string repositoryName = null)
25+
{
2126
var container = new Container();
2227
container.Configure(config =>
2328
{

SharpRepository.Sample.CoreMvc/Controllers/ContactsController.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Mvc;
77
using SharpRepository.Repository;
8-
using SharpRepository.CoreWebClient.Models;
8+
using SharpRepository.CoreMvc.Models;
99

10-
namespace SharpRepository.CoreWebClient.Controllers
10+
namespace SharpRepository.CoreMvc.Controllers
1111
{
1212
public class ContactsController : Controller
1313
{
14-
protected IRepository<Contact, int> repository;
14+
protected IRepository<Contact, string> repository;
1515

16-
public ContactsController(IRepository<Contact, int> repository)
16+
public ContactsController(IRepository<Contact, string> repository)
1717
{
1818
this.repository = repository;
1919
}
@@ -27,7 +27,7 @@ public ActionResult Index()
2727
}
2828

2929
// GET: Contacts/Details/5
30-
public ActionResult Details(int id)
30+
public ActionResult Details(string id)
3131
{
3232
var contact = repository.Get(id);
3333

@@ -45,20 +45,13 @@ public ActionResult Create()
4545
[ValidateAntiForgeryToken]
4646
public ActionResult Create(Contact contact)
4747
{
48-
try
49-
{
50-
repository.Add(contact);
48+
repository.Add(contact);
5149

52-
return RedirectToAction(nameof(Index));
53-
}
54-
catch
55-
{
56-
return View();
57-
}
50+
return RedirectToAction(nameof(Index));
5851
}
5952

6053
// GET: Contacts/Edit/5
61-
public ActionResult Edit(int id)
54+
public ActionResult Edit(string id)
6255
{
6356
var contact = repository.Get(id);
6457
return View(contact);
@@ -67,7 +60,7 @@ public ActionResult Edit(int id)
6760
// POST: Contacts/Edit/5
6861
[HttpPost]
6962
[ValidateAntiForgeryToken]
70-
public ActionResult Edit(int id, Contact contact)
63+
public ActionResult Edit(string id, Contact contact)
7164
{
7265
try
7366
{
@@ -82,7 +75,7 @@ public ActionResult Edit(int id, Contact contact)
8275
}
8376

8477
// GET: Contacts/Delete/5
85-
public ActionResult Delete(int id)
78+
public ActionResult Delete(string id)
8679
{
8780
var contact = repository.Get(id);
8881

@@ -92,7 +85,7 @@ public ActionResult Delete(int id)
9285
// POST: Contacts/Delete/5
9386
[HttpPost]
9487
[ValidateAntiForgeryToken]
95-
public ActionResult Delete(int id, IFormCollection collection)
88+
public ActionResult Delete(string id, IFormCollection collection)
9689
{
9790
try
9891
{

SharpRepository.Sample.CoreMvc/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
using System.Linq;
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.Mvc;
7-
using SharpRepository.CoreWebClient.Models;
7+
using SharpRepository.CoreMvc.Models;
88
using Microsoft.Extensions.Caching.Memory;
99
using SharpRepository.Repository;
1010

11-
namespace SharpRepository.CoreWebClient.Controllers
11+
namespace SharpRepository.CoreMvc.Controllers
1212
{
1313
public class HomeController : Controller
1414
{

SharpRepository.Sample.CoreMvc/Migrations/20171030091855_InitialContacts.Designer.cs

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Microsoft.EntityFrameworkCore.Metadata;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace SharpRepository.Samples.CoreMvc.Migrations
7+
{
8+
public partial class InitialContacts : Migration
9+
{
10+
protected override void Up(MigrationBuilder migrationBuilder)
11+
{
12+
migrationBuilder.CreateTable(
13+
name: "Contacts",
14+
columns: table => new
15+
{
16+
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
17+
Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
18+
},
19+
constraints: table =>
20+
{
21+
table.PrimaryKey("PK_Contacts", x => x.Id);
22+
});
23+
24+
migrationBuilder.CreateTable(
25+
name: "Email",
26+
columns: table => new
27+
{
28+
Id = table.Column<int>(type: "int", nullable: false)
29+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
30+
ContactId = table.Column<string>(type: "nvarchar(450)", nullable: true),
31+
EmailAddress = table.Column<string>(type: "nvarchar(max)", nullable: true)
32+
},
33+
constraints: table =>
34+
{
35+
table.PrimaryKey("PK_Email", x => x.Id);
36+
table.ForeignKey(
37+
name: "FK_Email_Contacts_ContactId",
38+
column: x => x.ContactId,
39+
principalTable: "Contacts",
40+
principalColumn: "Id",
41+
onDelete: ReferentialAction.Restrict);
42+
});
43+
44+
migrationBuilder.CreateIndex(
45+
name: "IX_Email_ContactId",
46+
table: "Email",
47+
column: "ContactId");
48+
}
49+
50+
protected override void Down(MigrationBuilder migrationBuilder)
51+
{
52+
migrationBuilder.DropTable(
53+
name: "Email");
54+
55+
migrationBuilder.DropTable(
56+
name: "Contacts");
57+
}
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// <auto-generated />
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Infrastructure;
4+
using Microsoft.EntityFrameworkCore.Metadata;
5+
using Microsoft.EntityFrameworkCore.Migrations;
6+
using Microsoft.EntityFrameworkCore.Storage;
7+
using Microsoft.EntityFrameworkCore.Storage.Internal;
8+
using SharpRepository.CoreMvc;
9+
using System;
10+
11+
namespace SharpRepository.Samples.CoreMvc.Migrations
12+
{
13+
[DbContext(typeof(ContactContext))]
14+
partial class ContactContextModelSnapshot : ModelSnapshot
15+
{
16+
protected override void BuildModel(ModelBuilder modelBuilder)
17+
{
18+
#pragma warning disable 612, 618
19+
modelBuilder
20+
.HasAnnotation("ProductVersion", "2.0.0-rtm-26452")
21+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
22+
23+
modelBuilder.Entity("SharpRepository.CoreMvc.Models.Contact", b =>
24+
{
25+
b.Property<string>("Id")
26+
.ValueGeneratedOnAdd();
27+
28+
b.Property<string>("Name");
29+
30+
b.HasKey("Id");
31+
32+
b.ToTable("Contacts");
33+
});
34+
35+
modelBuilder.Entity("SharpRepository.CoreMvc.Models.Email", b =>
36+
{
37+
b.Property<int>("Id")
38+
.ValueGeneratedOnAdd();
39+
40+
b.Property<string>("ContactId");
41+
42+
b.Property<string>("EmailAddress");
43+
44+
b.HasKey("Id");
45+
46+
b.HasIndex("ContactId");
47+
48+
b.ToTable("Email");
49+
});
50+
51+
modelBuilder.Entity("SharpRepository.CoreMvc.Models.Email", b =>
52+
{
53+
b.HasOne("SharpRepository.CoreMvc.Models.Contact")
54+
.WithMany("Emails")
55+
.HasForeignKey("ContactId");
56+
});
57+
#pragma warning restore 612, 618
58+
}
59+
}
60+
}

SharpRepository.Sample.CoreMvc/Models/Contact.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
using System;
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
using SharpRepository.Repository;
4+
using System;
25
using System.Collections.Generic;
6+
using System.ComponentModel.DataAnnotations;
37
using System.Linq;
48
using System.Threading.Tasks;
59

6-
namespace SharpRepository.CoreWebClient.Models
10+
namespace SharpRepository.CoreMvc.Models
711
{
812
public class Contact
913
{
10-
public int Id { get; set; }
14+
[BsonId] // Needed for MongoDB
15+
[BsonRepresentation(BsonType.ObjectId)] // Needed for MongoDB
16+
[RepositoryPrimaryKey] //Autogenrates value for strings
17+
[Key] //Ef primary key
18+
public string Id { get; set; }
1119
public string Name { get; set; }
1220
public virtual ICollection<Email> Emails { get; set; }
1321
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using SharpRepository.CoreMvc.Models;
3+
4+
namespace SharpRepository.CoreMvc
5+
{
6+
public class ContactContext : DbContext
7+
{
8+
public ContactContext(DbContextOptions<ContactContext> options) : base(options)
9+
{
10+
}
11+
12+
public DbSet<Contact> Contacts { get; set; }
13+
}
14+
}

SharpRepository.Sample.CoreMvc/Models/Email.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace SharpRepository.CoreWebClient.Models
1+
namespace SharpRepository.CoreMvc.Models
22
{
33
public class Email
44
{

0 commit comments

Comments
 (0)