diff --git a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs
index c7d8cd6..0f7f50c 100644
--- a/ASP.Net Core/MVCBasics/Controllers/HomeController.cs
+++ b/ASP.Net Core/MVCBasics/Controllers/HomeController.cs
@@ -6,10 +6,23 @@ namespace EntityFrameworkBasics.Controllers
{
public class HomeController : Controller
{
+
+ #region Protected members
+
+ protected ApplicationDBContext context;
+ #endregion
+
+ ///
+ /// Default constructor
+ ///
+ /// the injected context
+ public HomeController(ApplicationDBContext _context)
+ {
+ context = _context;
+ }
+
public IActionResult Index()
{
- // No using statement because of the DI and IoC
- var context = IoC.ApplicationDbContext;
context.Database.EnsureCreated();
diff --git a/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs
index 7d0e18d..a51d47e 100644
--- a/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs
+++ b/ASP.Net Core/MVCBasics/Data/ApplicationDBContext.cs
@@ -12,8 +12,6 @@ namespace EntityFrameworkBasics
public class ApplicationDBContext : DbContext
{
- public string Id { get; set; } = Guid.NewGuid().ToString("N");
-
#region Public properties
public DbSet Settings { get; set; }
@@ -36,6 +34,9 @@ namespace EntityFrameworkBasics
{
base.OnModelCreating(modelBuilder);
+ // Fluent Api
+ modelBuilder.Entity().HasIndex(c => c.Name).IsUnique();
+
}
}
}
diff --git a/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs b/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs
index c96085d..8610869 100644
--- a/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs
+++ b/ASP.Net Core/MVCBasics/Data/SettingsDataModel.cs
@@ -15,7 +15,6 @@ namespace EntityFrameworkBasics
///
/// The settings name
///
- [Required]
[MaxLength(256)]
public string Name { get; set; }
///
diff --git a/ASP.Net Core/MVCBasics/Program.cs b/ASP.Net Core/MVCBasics/Program.cs
index 4e98cab..82320c4 100644
--- a/ASP.Net Core/MVCBasics/Program.cs
+++ b/ASP.Net Core/MVCBasics/Program.cs
@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore;
+using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace EntityFrameworkBasics
@@ -12,9 +13,7 @@ namespace EntityFrameworkBasics
public static IWebHost BuildWebHost(string[] args)
{
- return new WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(Directory.GetCurrentDirectory())
+ return WebHost.CreateDefaultBuilder()
.UseStartup()
.Build();
}
diff --git a/ASP.Net Core/MVCBasics/Startup.cs b/ASP.Net Core/MVCBasics/Startup.cs
index 77a53dd..8059be6 100644
--- a/ASP.Net Core/MVCBasics/Startup.cs
+++ b/ASP.Net Core/MVCBasics/Startup.cs
@@ -24,7 +24,7 @@ namespace EntityFrameworkBasics
{
// Add ApplicationDbContext to DI
services.AddDbContext(options =>
- options.UseSqlServer("Server=192.168.0.135;Database=entityframework;User ID=sa;Password=SAtfoubuntu1SA;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"));
+ options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
diff --git a/ASP.Net Core/MVCBasics/appsettings.json b/ASP.Net Core/MVCBasics/appsettings.json
index 5fff67b..6b56936 100644
--- a/ASP.Net Core/MVCBasics/appsettings.json
+++ b/ASP.Net Core/MVCBasics/appsettings.json
@@ -1,4 +1,7 @@
{
+ "ConnectionStrings": {
+ "DefaultConnection": "Server=192.168.0.135;Database=entityframework;User ID=sa;Password=SAtfoubuntu1SA;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;MultipleActiveResultSets=true;"
+ },
"Logging": {
"IncludeScopes": false,
"LogLevel": {