040 Database access for Lookup resource of PhbkDivisionView - chempkovsky/CS82ANGULAR GitHub Wiki
- LpdDivision entity
- LprDivision01 entity
- LprDivision02 entity
- LpPhbkDbContext
- Regenerate LpdDivisionView and LpdDivisionViewPage
- Regenerate LprDivision01View and LprDivision01ViewPage
- Regenerate LprDivision02View and LprDivision02ViewPage
- In the article 035 we created three Entities, but at the end we removed them from the project. Those Entities were used more the modeling only.
- Now it's time to create the entities and DbContext that will be used by the lookup resource to access the database.
-
LpPhBkEntity
-project will be used for the entities -
LpPhBkContext
-project will be used for DbContext
-
- We are going to create additional DbContext.
- A new
Wizard's Repo
will be created for such a DbContext as soon as we generate the first ViewModel for any newly declared entity.
- A new
- The names and scalar properties of the newly declared entities must be the same as those of the entities created for modeling.
- with Visual Studio 2022 add
LpdDivision.cs
-file (class file) in thePhBk
-folder ofLpPhBkEntity
-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LpPhBkEntity.PhBk
{
public class LpdDivision
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Description = "Row id", Name = "Id of the Row", Prompt = "Enter Row Id", ShortName = "Row Id")]
[Required]
public int DivisionNameId { get; set; }
[Display(Description = "Name of the Enterprise Division", Name = "Name of the Division", Prompt = "Enter Division Name", ShortName = "Division Name")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "Invalid")]
[Required]
public string DivisionName { get; set; } = null!;
public List<LprDivision01> DivisionRef01 { get; set; } = null!;
public List<LprDivision02> DivisionRef02 { get; set; } = null!;
}
}
- with Visual Studio 2022 add
LprDivision01.cs
-file (class file) in thePhBk
-folder ofLpPhBkEntity
-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LpPhBkEntity.PhBk
{
public class LprDivision01
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
[Required]
public int DivisionId { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
[Required]
public int DivisionNameIdRef { get; set; }
public LpdDivision DivisionNameDict { get; set; } = null!;
}
}
-
Note: the following two line of code is gone!!!
public PhbkDivision Division { get; set; } = null!;
- with Visual Studio 2022 add
LprDivision02.cs
-file (class file) in thePhBk
-folder ofLpPhBkEntity
-project
Click to show the code
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LpPhBkEntity.PhBk
{
public class LprDivision02
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Description = "Row id", Name = "Id of the Division", Prompt = "Enter Division Id", ShortName = "Division Id")]
[Required]
public int DivisionId { get; set; }
[Display(Description = "Row id", Name = "Id of the Enterprise", Prompt = "Enter Enterprise Id", ShortName = "Enterprise Id")]
[Required]
public int EntrprsIdRef { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Display(Description = "Row id", Name = "Id of the DivisionName Row", Prompt = "Enter DivisionName Row Id", ShortName = "DivisionName Row Id")]
[Required]
public int DivisionNameIdRef { get; set; }
public LpdDivision DivisionNameDict { get; set; } = null!;
}
}
-
Note: the following two lines of code are gone!!!
public PhbkDivision Division { get; set; } = null!;
public PhbkEnterprise Enterprise { get; set; } = null!;
-
right click
PhBk
-folder of theLpPhBkContext
-project and runDbContext Wizard
.- name the newly created DbContext as LpPhbkDbContext
- add to LpPhbkDbContext
- LpdDivision entity which is created in the
LpPhBkEntity
-project- repeat the steps described in the article Modification related to LpdDivision
- LprDivision01 entity which is created in the
LpPhBkEntity
-project- repeat the steps described in the article Modification related to LprDivision01
- Note: You do not require to configure removed foreign keys
- LprDivision02 entity which is created in the
LpPhBkEntity
-project- repeat the steps described in the article Modification related to LprDivision02
- Note: You do not require to configure removed foreign keys
- LpdDivision entity which is created in the
-
Here s a result
Click to show the code
using Microsoft.EntityFrameworkCore;
using LpPhBkEntity.PhBk;
namespace LpPhBkContext.PhBk
{
public class LpPhbkDbContext : DbContext
{
public LpPhbkDbContext(DbContextOptions<LpPhbkDbContext> options)
: base(options)
{
Database.EnsureCreated();
}
//////////////////////////////////////////////////////////////////
/// reset ".UseSqlServer"
/// reset "ConfigurationManager.ConnectionStrings"
/// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
/////////////////////////////////////////////////////////////////
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// {
// optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
// }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<LprDivision02>().HasKey(p => new { p.EntrprsIdRef, p.DivisionNameIdRef, p.DivisionId });
modelBuilder.Entity<LprDivision01>().HasKey(p => new { p.DivisionNameIdRef, p.DivisionId });
modelBuilder.Entity<LpdDivision>().HasAlternateKey(p => p.DivisionName).HasName("LpdDivisionNameUk");
modelBuilder.Entity<LpdDivision>().HasKey(p => p.DivisionNameId);
modelBuilder.Entity<LprDivision01>().HasOne(d => d.DivisionNameDict)
.WithMany(m => m.DivisionRef01)
.HasForeignKey(d => d.DivisionNameIdRef)
.HasPrincipalKey(p => p.DivisionNameId)
.IsRequired(true)
.OnDelete(DeleteBehavior.NoAction);
modelBuilder.Entity<LprDivision02>().HasOne(d => d.DivisionNameDict)
.WithMany(m => m.DivisionRef02)
.HasForeignKey(d => d.DivisionNameIdRef)
.HasPrincipalKey(p => p.DivisionNameId)
.IsRequired(true)
.OnDelete(DeleteBehavior.NoAction);
}
public DbSet<LpdDivision> LpdDivisionDbSet {
get => Set<LpdDivision>();
}
public DbSet<LprDivision01> LprDivision01DbSet {
get => Set<LprDivision01>();
}
public DbSet<LprDivision02> LprDivision02DbSet {
get => Set<LprDivision02>();
}
}
}
- right click
PhBk
-folder of theLpPhBkViews
-project and runModelViews Wizard
- select
LpPhBkContext.csproj
,LpPhbkDbContext
on the second page - select
LpPhBkEntity.csproj
,LpdDivision
on the third page- repeat the steps described in the article Views for LpdDivision
Click to show the picture
-
Note: The generated C# code will be the same as at modeling time. But the new
Wizard's repo
will be created for the LpPhbkDbContext.
- right click
PhBk
-folder of theLpPhBkViews
-project and runModelViews Wizard
- select
LpPhBkContext.csproj
,LpPhbkDbContext
on the second page - select
LpPhBkEntity.csproj
,LprDivision01
on the third page- repeat the steps described in the article Views for LprDivision01
- Note: You do not require to configure foreign keys which are removed for the current entity's version.
- repeat the steps described in the article Views for LprDivision01
Click to show the picture
-
Note: The generated C# code will be the same as at modeling time. But the new
Wizard's repo
will be updated for the LpPhbkDbContext.
- right click
PhBk
-folder of theLpPhBkViews
-project and runModelViews Wizard
- select
LpPhBkContext.csproj
,LpPhbkDbContext
on the second page - select
LpPhBkEntity.csproj
,LprDivision02
on the third page- repeat the steps described in the article Views for LprDivision02
- Note: You do not require to configure foreign keys which are removed for the current entity's version.
- repeat the steps described in the article Views for LprDivision02
Click to show the picture
-
Note: The generated C# code will be the same as at modeling time. But the new
Wizard's repo
will be updated for the LpPhbkDbContext.