0% found this document useful (0 votes)
4 views

C# MVC ADD DB Connection String

Uploaded by

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

C# MVC ADD DB Connection String

Uploaded by

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

ssStep 1 - Add Connection in Config

<connectionStrings>
<add connectionString="Server=MANISH-PC;Database=OnlineExam;User
ID=sa;Password=pass@word1" name="default" providerName="System.Data.SqlClient" />
</connectionStrings>

STEP 2: CREATE MODEL


using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace TESTMVCPROJECT.Models
{
[Table("tbl_category")]

public class CategoryModel


{

[Key] public int Id { get; set; }


public string Categoryname { get; set; }
public int IsActive { get; set; }

}
}

STEP 3: CREATE DBCONTEXT


public class AddDBContext : DbContext
{

public AddDBContext() : base("default")


{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)


{
modelBuilder.Entity<CategoryModel>().ToTable("tbl_category");
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
public DbSet<CategoryModel> categories { get; set; }
}

STEP 4: CALL THE CONTEXT


AddDBContext con=new AddDBContext ();
var v = con.categories.ToList();

You might also like