Aggiunta progetto Data
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Controllers\BlaServApp.Data.DbController.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="5.0.1" />
|
||||
<PackageReference Include="NLog" Version="4.7.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Resources\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -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<BlaServAppContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
|
||||
#region Public Properties
|
||||
|
||||
public virtual DbSet<Articoli> DbSetArticoli { get; set; }
|
||||
public virtual DbSet<Config> 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<Config>(entity =>
|
||||
{
|
||||
entity.Property(e => e.ValoreStd)
|
||||
.HasComment("Valore di default/riferimento per la variabile");
|
||||
});
|
||||
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
#endregion Protected Methods
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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<DatabaseModels.Articoli> getArticoli()
|
||||
{
|
||||
var dbResult = dbCtx
|
||||
.DbSetArticoli
|
||||
.ToList();
|
||||
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
public List<DatabaseModels.Config> getConfig()
|
||||
{
|
||||
var dbResult = dbCtx
|
||||
.DbSetConfig
|
||||
.ToList();
|
||||
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace BlaServApp.Data.DatabaseModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Tabella Articoli
|
||||
/// </summary>
|
||||
[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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Tabella Articoli
|
||||
/// </summary>
|
||||
[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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace BlaServApp.UI.Data
|
||||
{
|
||||
public class Enum
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user