diff --git a/BlaServApp.Data/BlaServApp.Data.csproj b/BlaServApp.Data/BlaServApp.Data.csproj new file mode 100644 index 0000000..eae697c --- /dev/null +++ b/BlaServApp.Data/BlaServApp.Data.csproj @@ -0,0 +1,30 @@ + + + + net5.0 + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + \ No newline at end of file diff --git a/BlaServApp.Data/BlaServAppContext.cs b/BlaServApp.Data/BlaServAppContext.cs new file mode 100644 index 0000000..61c3b9f --- /dev/null +++ b/BlaServApp.Data/BlaServAppContext.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.Extensions.Configuration; +using BlaServApp.Data.DatabaseModels; +using NLog; + +namespace BlaServApp.Data +{ + public partial class BlaServAppContext : DbContext + { + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private IConfiguration _configuration; + + #endregion Private Fields + + #region Public Constructors + + public BlaServAppContext() + { + } + + public BlaServAppContext(IConfiguration configuration) + { + _configuration = configuration; + } + + public BlaServAppContext(DbContextOptions options) : base(options) + { + } + + #endregion Public Constructors + + #region Public Properties + + public virtual DbSet DbSetArticoli { get; set; } + public virtual DbSet DbSetConfig { get; set; } + + #endregion Public Properties + + #region Private Methods + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + #endregion Private Methods + + #region Protected Methods + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + //string connString = ""; + if (!optionsBuilder.IsConfigured) + { + //connString = _configuration.GetConnectionString("BlaServApp.Data"); + //optionsBuilder.UseSqlServer(connString); + optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=BlaServApp;Trusted_Connection=True;"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS"); + + modelBuilder.Entity(entity => + { + entity.Property(e => e.ValoreStd) + .HasComment("Valore di default/riferimento per la variabile"); + }); + + OnModelCreatingPartial(modelBuilder); + } + + #endregion Protected Methods + } +} \ No newline at end of file diff --git a/BlaServApp.Data/Constants.cs b/BlaServApp.Data/Constants.cs new file mode 100644 index 0000000..fe1065b --- /dev/null +++ b/BlaServApp.Data/Constants.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlaServApp.Data +{ + public class Constants + { + } +} \ No newline at end of file diff --git a/BlaServApp.Data/Controllers/BlaServAppController.cs b/BlaServApp.Data/Controllers/BlaServAppController.cs new file mode 100644 index 0000000..f2285dd --- /dev/null +++ b/BlaServApp.Data/Controllers/BlaServAppController.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using NLog; + +namespace BlaServApp.Data.Controllers +{ + public class BlaServAppController : IDisposable + { + #region Private Fields + + private static IConfiguration _configuration; + private static BlaServAppContext dbCtx; + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + + #region Public Constructors + + public BlaServAppController(IConfiguration configuration) + { + _configuration = configuration; + dbCtx = new BlaServAppContext(configuration); + Log.Info("Avviata classe BlaServAppController"); + } + + #endregion Public Constructors + + #region Public Methods + + public void Dispose() + { + // Clear database context + dbCtx.Dispose(); + } + + public List getArticoli() + { + var dbResult = dbCtx + .DbSetArticoli + .ToList(); + + return dbResult; + } + + public List getConfig() + { + var dbResult = dbCtx + .DbSetConfig + .ToList(); + + return dbResult; + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/BlaServApp.Data/DatabaseModels/Articoli.cs b/BlaServApp.Data/DatabaseModels/Articoli.cs new file mode 100644 index 0000000..3ed8625 --- /dev/null +++ b/BlaServApp.Data/DatabaseModels/Articoli.cs @@ -0,0 +1,33 @@ +using System; +using System.Linq; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace BlaServApp.Data.DatabaseModels +{ + /// + /// Tabella Articoli + /// + [Table("Articoli")] + public class Articoli + { + #region Public Properties + + [Key, Column("ArtId", Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int ArtId { get; set; } + + [Column("CodArticolo", Order = 1)] + public string CodArticolo { get; set; } = ""; + + [Column("DescArticolo", Order = 2)] + public string DescArticolo { get; set; } = ""; + + [Column("Tipo", Order = 3)] + public string Tipo { get; set; } = ""; + + [Column("UM", Order = 4)] + public string UM { get; set; } = ""; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/BlaServApp.Data/DatabaseModels/Config.cs b/BlaServApp.Data/DatabaseModels/Config.cs new file mode 100644 index 0000000..340b358 --- /dev/null +++ b/BlaServApp.Data/DatabaseModels/Config.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +#nullable disable + +namespace BlaServApp.Data.DatabaseModels +{ + /// + /// Tabella Articoli + /// + [Table("Config")] + public class Config + { + #region Public Properties + + [Key, Column("Chiave", Order = 0), MaxLength(50)] + public string Chiave { get; set; } + + [Column("Note", Order = 3)] + public string Note { get; set; } = ""; + + [Column("Valore", Order = 1)] + public string Valore { get; set; } = ""; + + [Column("ValoreStd", Order = 2)] + public string ValoreStd { get; set; } = ""; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/BlaServApp.Data/Enum.cs b/BlaServApp.Data/Enum.cs new file mode 100644 index 0000000..ce1f0ad --- /dev/null +++ b/BlaServApp.Data/Enum.cs @@ -0,0 +1,8 @@ +using System; + +namespace BlaServApp.UI.Data +{ + public class Enum + { + } +} \ No newline at end of file diff --git a/BlaServApp.Data/Resources/Readme.md b/BlaServApp.Data/Resources/Readme.md new file mode 100644 index 0000000..77ffa58 --- /dev/null +++ b/BlaServApp.Data/Resources/Readme.md @@ -0,0 +1,26 @@ +# Appunti gestione BlaServApp DB + + +Per la gestione dell'accesso al DB si opera con EFCore --> app blazor server + +## Scaffolding + +Per generare le classi da un DB esistente con cui operare EFCore CodeFirst usare lo scaffolding coi seguenti comandi. +Attenzione: la classe DbCOntext viene creata INSIEME alle viste nella folder DatabaseModel (nell'esempio seguente...) + +### DB iniziale + + Scaffold-DbContext "Server=SQL2016DEV;Database=BlaServApp;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DatabaseModels + +### SOLO di tabelle/viste selezionate (con force update) + +Scaffold-DbContext "Server=SQL2016DEV;Database=BlaServApp;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DatabaseModels -Tables nome_tabella, nome_vista + +## Approfondimenti + +Qualche link di approfondimento: + + - https://docs.microsoft.com/en-us/ef/core/ + - https://docs.microsoft.com/en-us/ef/core/extensions/ + - https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx + - https://entityframework.net/ef-code-first \ No newline at end of file