0% found this document useful (0 votes)
23 views3 pages

Any Kind 2

The configuration class is applied automatically during migration without calling ApplyConfiguration due to inheritance. The virtual keyword is important for navigation properties to enable lazy loading but not needed for DbSet properties in the context. To use a stored procedure, define a DTO class to match the result, add a DbSet property in the context for the DTO, configure it to have no key, call FromSqlRaw to execute the stored procedure and return the result.

Uploaded by

ghannam384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Any Kind 2

The configuration class is applied automatically during migration without calling ApplyConfiguration due to inheritance. The virtual keyword is important for navigation properties to enable lazy loading but not needed for DbSet properties in the context. To use a stored procedure, define a DTO class to match the result, add a DbSet property in the context for the DTO, configure it to have no key, call FromSqlRaw to execute the stored procedure and return the result.

Uploaded by

ghannam384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

*The configuraytion applied Automattically without any line of code because you

inheret from class


-the LoyaltySetupDetailConfiguration class will be applied to the
LoyaltySetupDetail entity model during the migration process
-without explicitly calling "modelBuilder.ApplyConfiguration(new
LoyaltySetupDetailConfiguration())" in the OnModelCreating method.

*virtual keyword important in Navigation Prop, but not in Context.cs


-"public virtual DbSet<LoyaltySetupDetail> LoyaltySetupDetails { get; set; }"
in Context.cs (Generated Tables from Scafollding)
-In Entity Framework Core, the virtual keyword is used in the context of
virtual DbSet<T> to enable certain features related to change tracking and lazy
loading.

-"public virtual ICollection<LoyaltySetupDetail> LoyaltySetupDetails { get;


set; }" (Navigation Prop)
-In this case, the virtual keyword allows for the possibility of lazy
loading,
meaning that related entities are loaded from the database only when you
access the navigation property. Without virtual, lazy loading won't be supported
for that property.

*The steps to deal with SP in SQL Server

1-Create SP in SQL Server Query

2- Create a model Dto to match the result of SP

3-Create a table in Context class to call it with _context :


"public DbSet<RetailCustomerLoyaltyPointsSPDto>
RetailCustomerLoyaltyPointsSPDtos { get; set; }"

-in onModelCreatingMEthod : to deal with this model ot table need to difine it


has no key to can deal with this table
modelBuilder.Entity<RetailCustomerLoyaltyPointsSPDto>(e =>
{
e.HasNoKey();
e.ToView(null); // or Replace with the actual stored procedure name
});

4- in Controoler : var Paramter = new SqlParameter("NameSring", Value);


// var result = dbContext."YourDTOClass".FromSqlRaw(@"EXEC
MyStoredProcudureName",paramter).ToList();

[HttpGet("PointsDtos")]
public async Task<ActionResult<RetailCustomers>>
GetCustomerPointsFromDtos(string phone)
{
try
{
var Paramter = new SqlParameter("Phone", phone);
var resurt = await Task.Run(() =>
_context.CustomerPointsReadSPDtos.FromSqlRaw(@"Exec GetRetailCustomerLoyaltyPoints
@Phone", Paramter).ToListAsync());
if (resurt == null)
{
return BadRequest();
}
return Ok(resurt*The configuraytion applied Automattically without
any line of code because you inheret from class
-the LoyaltySetupDetailConfiguration class will be applied to the
LoyaltySetupDetail entity model during the migration process
-without explicitly calling "modelBuilder.ApplyConfiguration(new
LoyaltySetupDetailConfiguration())" in the OnModelCreating method.

*virtual keyword important in Navigation Prop, but not in Context.cs


-"public virtual DbSet<LoyaltySetupDetail> LoyaltySetupDetails { get; set; }"
in Context.cs (Generated Tables from Scafollding)
-In Entity Framework Core, the virtual keyword is used in the context of
virtual DbSet<T> to enable certain features related to change tracking and lazy
loading.

-"public virtual ICollection<LoyaltySetupDetail> LoyaltySetupDetails { get;


set; }" (Navigation Prop)
-In this case, the virtual keyword allows for the possibility of lazy
loading,
meaning that related entities are loaded from the database only when you
access the navigation property. Without virtual, lazy loading won't be supported
for that property.

*The steps to deal with SP in SQL Server

1-Create SP in SQL Server Query

2- Create a model Dto to match the result of SP

3-Create a table in Context class to call it with _context :


"public DbSet<RetailCustomerLoyaltyPointsSPDto>
RetailCustomerLoyaltyPointsSPDtos { get; set; }"

-in onModelCreatingMEthod : to deal with this model ot table need to difine it


has no key to can deal with this table
modelBuilder.Entity<RetailCustomerLoyaltyPointsSPDto>(e =>
{
e.HasNoKey();
e.ToView(null); // or Replace with the actual stored procedure name
});

4- in Controoler : var Paramter = new SqlParameter("NameSring", Value);


// var result = dbContext."YourDTOClass".FromSqlRaw(@"EXEC
MyStoredProcudureName",paramter).ToList();

[HttpGet("PointsDtos")]
public async Task<ActionResult<RetailCustomers>>
GetCustomerPointsFromDtos(string phone)
{
try
{
var Paramter = new SqlParameter("Phone", phone);
var resurt = await Task.Run(() =>
_context.CustomerPointsReadSPDtos.FromSqlRaw(@"Exec GetRetailCustomerLoyaltyPoints
@Phone", Paramter).ToListAsync());
if (resurt == null)
{
return BadRequest();
}
return Ok(resurt

You might also like