Azure

Exploring Azure Features HTTP Set off Utilizing EF Core

The HTTP set off permits you to invoke a operate with an HTTP request. These HTTP triggers allow you to construct a serverless API  and reply to the webhooks.

 

Coding

 

Let’s start by creating a brand new Azure Features challenge by choose the set off kind as HTTP.

 

Add Nuget package deal

  1. Microsoft.EntityFrameworkCore.SqlServer  
  2. Microsoft.EntityFrameworkCore.Design  
  3. Microsoft.EntityFrameworkCore.Instruments  
  4. Microsoft.Azure.Features.Extensions  

Add the entity Mannequin,

  1. public class Worker {  
  2.     public int Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Identify {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public int Age {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public double Wage {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public string Metropolis {  
  19.         get;  
  20.         set;  
  21.     }  
  22.     public string State {  
  23.         get;  
  24.         set;  
  25.     }  
  26. }  

Subsequent, I’ll create a plain context for the info mannequin to work together.

  1. public class EmployeeContext: DbContext {  
  2.     public EmployeeContext(DbContextOptions < EmployeeContext > dbContextOptions): base(dbContextOptions) {}  
  3.     public DbSet < Worker > Workers {  
  4.         get;  
  5.         set;  
  6.     }  
  7. }  

Write Perform code to inject context

 

To inject the EmployeeContext in our HTTP Perform, we first have to register the context in configure technique of the FunctionStartUp.

  1. [assembly: FunctionsStartup(typeof(HttpTriggerVerify.Startup))]  
  2. namespace HttpTriggerVerify {  
  3.     public class Startup: FunctionsStartup {  
  4.         public override void Configure(IFunctionsHostBuilder builder) {  
  5.             string SqlConnection = Surroundings.GetEnvironmentVariable(“SqlConnectionString”);  
  6.             builder.Companies.AddDbContext < EmployeeContext > (x => x.UseSqlServer(SqlConnection));  
  7.         }  
  8.     }  
  9. }  

  1. public class HttpTriggerVerify {  
  2.     personal readonly EmployeeContext employeeContext;  
  3.     public HttpTriggerVerify(EmployeeContext employeeContext) {  
  4.             this.employeeContext = employeeContext;  
  5.         }  
  6.         [FunctionName(“HttpTriggerVerify”)]  
  7.     public IActionResult GetEmployees(  
  8.             [HttpTrigger(AuthorizationLevel.Anonymous, “get”)] HttpRequest req, ILogger log) {  
  9.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  10.             var staff = employeeContext.Workers.ToList();  
  11.             return new OkObjectResult(staff);  
  12.         }  
  13.         [FunctionName(“SaveEmployee”)]  
  14.     public async Job < ActionResult > SaveEmployeeAsync([HttpTrigger(AuthorizationLevel.Anonymous, “post”)] HttpRequest req ILogger log) {  
  15.         string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  16.         var information = JsonConvert.DeserializeObject < Worker > (requestBody);  
  17.         await employeeContext.Workers.AddAsync(information);  
  18.         await employeeContext.SaveChangesAsync();  
  19.         return new OkResult();  
  20.     }  
  21. }  

On this instance, we’re utilizing two capabilities

  • GetEmployees: Get all the staff from the DB
  • SaveEmployee: Insert the worker associated data to DB

Right here, I’m utilizing the Authorization degree as Nameless for simplicity functions.

 

Executing EF Core Migration

 

Now, run the EF core migration through the use of the under instructions.

  1.   
  2. dotnet ef migrations add InitialContext  
  3. For Home windows  
  4. add-migrations InitialContext  

After operating the migration command, an error is thrown suggesting it is unable to seek out the challenge dll within the netcoreapp folder. However, you will discover the challenge dll file contained in the netcoreapp’s bin folder.

 

Sadly, the design time instruments like EF core migration count on the dll’s to be current within the root of construct goal. To make the EF core migration comfortable, we have to add publish construct occasion to repeat the dll to the foundation of construct goal.

  1. <Goal Identify=“PostBuild” AfterTargets=“PostBuildEvent”>  
  2.    <Exec Command=“cp “$(TargetDir)bin$(ProjectName).dll” “$(TargetDir)$(ProjectName).dll“” />  
  3. </Goal>  

Once more after operating the migration script, EF core is now complaining about not having the ability to discover the specified context. We are able to repair it through the use of IDesignTimeDbContextFactory<T>.

  1. public class EmployeeContextFactory: IDesignTimeDbContextFactory < EmployeeContext > {  
  2.     public EmployeeContext CreateDbContext(string[] args) {  
  3.         var optionsBuilder = new DbContextOptionsBuilder < EmployeeContext > ();  
  4.         optionsBuilder.UseSqlServer(Surroundings.GetEnvironmentVariable(“SqlConnectionString”));  
  5.         return new EmployeeContext(optionsBuilder.Choices);  
  6.     }  
  7. }  

After operating the migration script, all the things appears to be working completely! Now, replace the database with the most recent migration

  1.   
  2. dotnet ef database replace  
  3.   
  4. update-database  

Now, I can see Worker desk is being added to the DB.

 

Lastly, we’ve managed to place all of the code adjustments in place. Run the appliance and confirm the GetEmployees and SaveEmployee strategies are working as anticipated.

 

I hope you just like the article. When you discovered this text attention-grabbing then kindly like and share it. 

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button