The entity framework course is finished

This commit is contained in:
2020-01-22 23:02:33 +01:00
parent cc8eb0302e
commit 8d11b258bd
6 changed files with 25 additions and 10 deletions

View File

@ -6,10 +6,23 @@ namespace EntityFrameworkBasics.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
#region Protected members
protected ApplicationDBContext context;
#endregion
/// <summary>
/// Default constructor
/// </summary>
/// <param name="_context">the injected context</param>
public HomeController(ApplicationDBContext _context)
{
context = _context;
}
public IActionResult Index() public IActionResult Index()
{ {
// No using statement because of the DI and IoC
var context = IoC.ApplicationDbContext;
context.Database.EnsureCreated(); context.Database.EnsureCreated();

View File

@ -12,8 +12,6 @@ namespace EntityFrameworkBasics
public class ApplicationDBContext : DbContext public class ApplicationDBContext : DbContext
{ {
public string Id { get; set; } = Guid.NewGuid().ToString("N");
#region Public properties #region Public properties
public DbSet<SettingsDataModel> Settings { get; set; } public DbSet<SettingsDataModel> Settings { get; set; }
@ -36,6 +34,9 @@ namespace EntityFrameworkBasics
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
// Fluent Api
modelBuilder.Entity<SettingsDataModel>().HasIndex(c => c.Name).IsUnique();
} }
} }
} }

View File

@ -15,7 +15,6 @@ namespace EntityFrameworkBasics
/// <summary> /// <summary>
/// The settings name /// The settings name
/// </summary> /// </summary>
[Required]
[MaxLength(256)] [MaxLength(256)]
public string Name { get; set; } public string Name { get; set; }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System.IO; using System.IO;
namespace EntityFrameworkBasics namespace EntityFrameworkBasics
@ -12,9 +13,7 @@ namespace EntityFrameworkBasics
public static IWebHost BuildWebHost(string[] args) public static IWebHost BuildWebHost(string[] args)
{ {
return new WebHostBuilder() return WebHost.CreateDefaultBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>() .UseStartup<Startup>()
.Build(); .Build();
} }

View File

@ -24,7 +24,7 @@ namespace EntityFrameworkBasics
{ {
// Add ApplicationDbContext to DI // Add ApplicationDbContext to DI
services.AddDbContext<ApplicationDBContext>(options => services.AddDbContext<ApplicationDBContext>(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(); services.AddMvc();
} }

View File

@ -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": { "Logging": {
"IncludeScopes": false, "IncludeScopes": false,
"LogLevel": { "LogLevel": {