Basic Sample Application Implementation in .Net Core using c# and Entity Frame work

First of all you will select .Net core in visual studio Template.




After that you may create your own  View .
Before creating the view ,you may add Create Data base and Tables .

CREATE TABLE [dbo].[BaseMaster](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](500) NULL,
[Address] [nvarchar](1000) NULL,
[Phone] [nvarchar](20) NULL,
[Email] [nvarchar](50) NULL,
[TimeStamp] [datetime] NULL DEFAULT (getdate()),
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


Then Insert some data to the table Using below queries

insert into BaseMaster values(newid(),'Test1', 'Hyderabad','040-0000000','sree@aaa.com');
insert into BaseMaster values(newid(),'Test2', 'Hyderabad','040-0000000','nivas@aaa.com');




then Create Models a/ Entities for your tables in Database .

After that right click on Manage nuget packages and add Entity framework core:



After that you may update the below version to latest version like 2.0 to 2.1 Versions



************************BaseEntities********************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace BestpracticeApplication.Models
{
    public class BaseEntities : DbContext
    {
        public BaseEntities(DbContextOptions<BaseEntities> options) : base(options)
        {
        }

        public DbSet<BaseMaster> BaseMaster { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<BaseMaster>().ToTable("CogniaMaster");
        }
    }
}
********************************************************
***************************BaseMaster*****************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;




namespace BestpracticeApplication.Models
{
   // [Table("BaseMaster")]
    public class BaseMaster
    {
       // [Key]
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public DateTime TimeStamp { get; set; }
    }
}


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

************************IBaseMasterService********************************


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BestpracticeApplication.Models
{
    public interface IBaseMasterService
    {
        BaseMaster GetBaseMaster(Guid id);
        List<BaseMaster> GetAllBaseMaster();

    }
}


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

************************BaseMasterService********************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BestpracticeApplication.Models
{
    public class BaseMasterService : IBaseMasterService
    {
        public BaseEntities _BaseEntities;
        public BaseMasterService(BaseEntities BaseEntities)
        {
            this._BaseEntities = BaseEntities;
        }

        public List<BaseMaster> GetAllBaseMaster()
        {
            return _BaseEntities.BaseMaster.ToList();
        }

        public BaseMaster GetBaseMaster(Guid id)
        {
            return _BaseEntities.BaseMaster.Where(a => a.Id == id).FirstOrDefault();
        }
    }
}

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

************************HomeController********************************
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BestpracticeApplication.Models;

namespace BestpracticeApplication.Controllers
{
    public class HomeController : Controller
    {
        public IBaseMasterService _BaseMasterService;
        public HomeController(IBaseMasterService BaseMasterService)
        {
            this._BaseMasterService = BaseMasterService;
        }
        public IActionResult Index()
        {
            return View();
        }
        
        [HttpGet]
        public IActionResult GetAllFirms()
        {
            var BaseMasters = _BaseMasterService.GetAllBaseMaster();
            return View(BaseMasters);
        }
    }
}

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

************************appsettings.Json********************************
{
  "ConnectionStrings": {
    "DefaultConnection": "data source=.;database=Base;user id=sa;password=ok@123"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}


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

************************Startup.cs********************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using BestpracticeApplication.Models;

namespace BestpracticeApplication
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddTransient<IBaseMasterService, BaseMasterService>();
            services.AddDbContext<BaseEntities>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=GetAllFirms}/{id?}");
            });
        }
    }
}


********************************************************
Then create one view for UI
**********************GetAllFirms.cshtml**********************************
@model IEnumerable<BestpracticeApplication.Models.BaseMaster>

@{
    ViewData["Title"] = "GetAllFirms";
}

<h2 style="text-align:center;">All Firm Details</h2>

<p style="text-align:end;">
    <a asp-action="Create">Create New</a>
</p>
<table class="table table-bordered" border="1">
    <thead class="text-capitalize">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Address)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Phone)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Email)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.TimeStamp)
            </th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Phone)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.TimeStamp)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
            </td>
        </tr>
}
    </tbody>
</table>
***********************************************************************

If your getting any issues , please update you .net core version 2.0 to 2.1

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

If you have any issue please comment below , i will surely reply your questions. 

Thank you all......


Comments

Popular posts from this blog

Reverse Sentence using c#

How to write Pure java script Program?