Wednesday, September 21, 2022

ASP NET CORE ENTITY FRAMEWORK CODE FIRST APPROACH

******************************************************************************

******************************************************************************

//For Entity Framework

Microsoft.EntityFrameworkCore

Microsoft.EntityFrameworkCore.SqlServer

Microsoft.EntityFrameworkCore.Tools

******************************************************************************

//For Swagger Installation

Install Swashbuckle.AspNetCore.SwaggerUI and Swashbuckle.AspNetCore.SwaggerGen.


Configure Services Method


 services.AddSwaggerGen(c =>

            {

                c.SwaggerDoc("v2", new OpenApiInfo { Title = "My API", Version = "v2" });

            });


Configure Method


 app.UseSwagger();

            app.UseSwaggerUI(c =>

            {

                c.SwaggerEndpoint("/swagger/v2/swagger.json", "My API V1");

            });

******************************************************************************

//For DbContext


public class DataContext: DbContext

    {

        public DataContext(DbContextOptions<DataContext> options): base(options)

        {


        }

        public DbSet<Department> Departments { get; set; }


// Adding Db Context in Configure Services Method in Startup class

services.AddDbContext<DataContext>(options =>

            {

                options.UseSqlServer(Configuration.GetConnectionString("myLocalDb"));

            });


******************************************************************************


// Specific Migration add

Add-Migration -context datacontext(class name in lowercase)


// specific database update

Update-Database -context datacontext


******************************************************************************

adding allowed CORS in Configure Serivces Method in Startup class

services.AddCors(c =>

            {

                c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

            });


using allowed CORS in Configure Method in Startup class



app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());


******************************************************************************



using static file option

in configure method in startup.cs file

app.UseStaticFiles(new StaticFileOptions

            {

                FileProvider = new PhysicalFileProvider(

                    Path.Combine(Directory.GetCurrentDirectory(),"Photos")),

                RequestPath = "/Photos"

            });


******************************************************************************


Adding Entity

public class DepartmentController : ControllerBase

    {

        private DataContext _dataContext;

        public DepartmentController(DataContext dataContext)

        {

            _dataContext = dataContext;

        }

        [HttpPost]

        public ActionResult<Department> AddDepartment(Department department)

        {

            _dataContext.Departments.Add(department);

            _dataContext.SaveChanges();

            return department;

        }

    }


******************************************************************************

connectionString in appsettings.json

{

  "Logging": {

    "LogLevel": {

      "Default": "Information",

      "Microsoft": "Warning",

      "Microsoft.Hosting.Lifetime": "Information"

    }

  },

  "ConnectionStrings": {

    "myLocalDb": "Server=(local)\\SQLEXPRESS;Database= CodeFirstDb;Trusted_Connection = true;MultipleActiveResultSets=True"

  },

  "AllowedHosts": "*"

}


******************************************************************************

******************************************************************************

No comments:

Post a Comment

Two Factor Authentication using .Net Core

Install Package dotnet add package GoogleAuthenticator --version 3.1.1 Model Changes public bool IsAuthenticatorReset { get; set; } public s...