connecting with database..
This commit is contained in:
parent
ee92564bf2
commit
0733b933da
|
|
@ -6,6 +6,7 @@ using System.Net.Http;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ProjectGrid.Data;
|
||||
|
||||
namespace ProjectGrid.Controllers
|
||||
{
|
||||
|
|
@ -15,7 +16,8 @@ namespace ProjectGrid.Controllers
|
|||
|
||||
private readonly ILogger<UsersController> _logger;
|
||||
|
||||
static readonly Models.IUserRepository repository = new Models.UserRepository();
|
||||
//static readonly Models.IUserRepository repository = new Models.UserRepository();
|
||||
static readonly Models.IUserRepository repository = DataAccessContext.GetInstance();
|
||||
|
||||
public UsersController(ILogger<UsersController> logger)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ProjectGrid.Models;
|
||||
|
|
@ -6,16 +7,55 @@ using ProjectGrid.Models;
|
|||
|
||||
namespace ProjectGrid.Data
|
||||
{
|
||||
public class DataAccessContext : DbContext
|
||||
public class DataAccessContext : DbContext, IUserRepository
|
||||
{
|
||||
private static DataAccessContext _instance;
|
||||
public static DataAccessContext GetInstance() => _instance;
|
||||
|
||||
public DataAccessContext(DbContextOptions<DataAccessContext> options) { }
|
||||
public DataAccessContext(DbContextOptions<DataAccessContext> options) : base(options)
|
||||
{
|
||||
_instance = this;
|
||||
}
|
||||
|
||||
public DbSet<UserModel> Users;
|
||||
public DbSet<UserData> Users { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<UserModel>().ToTable("[USER]");
|
||||
modelBuilder.Entity<UserData>().ToTable("USER");
|
||||
}
|
||||
|
||||
public IEnumerable<UserModel> GetAll()
|
||||
{
|
||||
return Users.Select<UserData, UserModel>( data => data.ToModel() );
|
||||
}
|
||||
|
||||
public UserModel Add(UserModel user)
|
||||
{
|
||||
// TODO: check if exist
|
||||
Users.Add(new UserData(user));
|
||||
SaveChanges();
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
public class UserData
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
public string Name1 { get; set; }
|
||||
public string Name2 { get; set; }
|
||||
public string Email { get; set; }
|
||||
|
||||
public UserData() { }
|
||||
public UserData(UserModel model)
|
||||
{
|
||||
Name1 = model.firstName;
|
||||
Name2 = model.lastName;
|
||||
Email = model.email;
|
||||
}
|
||||
|
||||
public UserModel ToModel()
|
||||
{
|
||||
return new UserModel {firstName = Name1, lastName = Name2, email = Email};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,24 +11,24 @@ namespace ProjectGrid.Data
|
|||
{
|
||||
context.Database.EnsureCreated();
|
||||
|
||||
// Look for any UserModels.
|
||||
// Look for any UserDatas.
|
||||
if (context.Users.Any())
|
||||
{
|
||||
return; // DB has been seeded
|
||||
}
|
||||
|
||||
var users = new UserModel[]
|
||||
var users = new UserData[]
|
||||
{
|
||||
new UserModel{firstName="Carson",lastName="Alexander",email="test@abc.com"},
|
||||
new UserModel{firstName="Meredith",lastName="Alonso",email="test@abc.com"},
|
||||
new UserModel{firstName="Arturo",lastName="Anand",email="test@abc.com"},
|
||||
new UserModel{firstName="Gytis",lastName="Barzdukas",email="test@abc.com"},
|
||||
new UserModel{firstName="Yan",lastName="Li",email="test@abc.com"},
|
||||
new UserModel{firstName="Peggy",lastName="Justice",email="test@abc.com"},
|
||||
new UserModel{firstName="Laura",lastName="Norman",email="test@abc.com"},
|
||||
new UserModel{firstName="Nino",lastName="Olivetto",email="test@abc.com"}
|
||||
new UserData{Name1="Carson",Name2="Alexander",Email="test@abc.com"},
|
||||
new UserData{Name1="Meredith",Name2="Alonso",Email="test@abc.com"},
|
||||
new UserData{Name1="Arturo",Name2="Anand",Email="test@abc.com"},
|
||||
new UserData{Name1="Gytis",Name2="Barzdukas",Email="test@abc.com"},
|
||||
new UserData{Name1="Yan",Name2="Li",Email="test@abc.com"},
|
||||
new UserData{Name1="Peggy",Name2="Justice",Email="test@abc.com"},
|
||||
new UserData{Name1="Laura",Name2="Norman",Email="test@abc.com"},
|
||||
new UserData{Name1="Nino",Name2="Olivetto",Email="test@abc.com"}
|
||||
};
|
||||
foreach (UserModel s in users)
|
||||
foreach (UserData s in users)
|
||||
{
|
||||
context.Users.Add(s);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ namespace ProjectGrid
|
|||
.ConfigureLogging(loggingBuilder =>
|
||||
{
|
||||
loggingBuilder.ClearProviders();
|
||||
loggingBuilder.AddDebug().AddEventLog();
|
||||
loggingBuilder.AddDebug().AddConsole();
|
||||
})
|
||||
.ConfigureWebHostDefaults(webBuilder =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,12 @@ namespace ProjectGrid
|
|||
{
|
||||
ILogger _logger;
|
||||
|
||||
public Startup() { }
|
||||
public Startup()
|
||||
{
|
||||
Configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json
|
||||
.Build();
|
||||
}
|
||||
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
|
@ -28,6 +33,8 @@ namespace ProjectGrid
|
|||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
var connectionString = Configuration.GetConnectionString("DefaultConnection");
|
||||
// Cant log here..
|
||||
//_logger.LogInformation($"Try to connect to Database with {connectionString}");
|
||||
services.AddDbContext<DataAccessContext>(options => options.UseSqlServer(connectionString));
|
||||
//_logger.LogInformation($"DataAccessContext registered with {connectionString}");
|
||||
// TODO: for Testing
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456"
|
||||
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;uid=net;pwd=15123456"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue