Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please let me know how make asp.net mvc with sql server.. i mean with out entity framework, and i am not supposed to use loacl database by importing database file. so i write connection string for sql server.. by this i want to do database opearations using DbContext..


here is my connection string


<add name="MyConnection" connectionstring="Data Source=v-rrd2005; User ID=sa;Password=pa55w0rd!;Initial Catalog=TrainingDB;Integrated Security=True" providername="System.Data.SqlClient">

but i am unable to connect sql server..


here is context.cs file


XML
public partial class MyConnection : DbContext
   {
       public MyConnection()
           : base("name=MyConnection")
       {
        //   Database.SetInitializer<DbContext>(null);
       }
       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
          // throw new UnintentionalCodeFirstException();
       }
       public DbSet<EmployeeInfo> TrainingDB { get; set; }
   }





here my EmployeeInfo is table name and TrainingDB is database name


my contorler page



public class GetEmpInfoController : ApiController
    {
        private MyConnection db = new MyConnection();

        // GET api/EmployeeInfoAPI
        public IEnumerable<employeeinfo> GetEmployeeInfoes()
        {
            return db.TrainingDB.AsEnumerable();
        }

        // GET api/EmployeeInfoAPI/5
        public EmployeeInfo GetEmployeeInfo(int id)
        {
            EmployeeInfo employeeinfo = db.TrainingDB.Find(id);
            if (employeeinfo == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return employeeinfo;
        }

    }


im unable to make connection to my sqlserver..
Posted
Updated 22-Jan-14 18:56pm
v2

1 solution

To use Entity Framework, you add an edmx to your project, which builds the classes you use. It also inserts the connection string in to your web.config.
 
Share this answer
 
Comments
Sanjeev Alamuri 23-Jan-14 1:23am    
My requirement is mvc with sqlserver.. not to use entity framework.
Christian Graus 23-Jan-14 1:30am    
"public partial class MyConnection : DbContext"

This is Entity Framework code. If you don't want to use EF, write a data layer by hand, that's your other option. Then you can put the connection string in your web.config, hard code it, put it where-ever you like.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900