1

I was wondering if anyone could help me with making a datacontext on existing database and searching from it.

What i've done so far:

  1. Made connectionstring for existing database on web.config (same name as my newly created DataContext class)
  2. Made DataContext class, and model class for it where are the fields i want to get.
  3. Made controller for it which calls for the search
  4. Made view for the controller

Here is the code i've used.

DataContext class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace KendoUIMvcCim.Models
{
    public class CustDataContext : DbContext
    {
        public DbSet<Contacts> CLIENT { get; set; }

    }
}

Model class for the information i want to search

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace KendoUIMvcCim.Models
{
    public class Contacts
    {
        [Key]
        public int CLIENT_ID { get; set; }
        public string FIRSTNAME { get; set; }
        public string LASTNAME { get; set; }
        public string TEL_TEL1 { get; set; }
        public string TEL_TEL2 { get; set; }
        public string TEL_GSM { get; set; }
    }
}

Controller

public ActionResult Testi(int? clientid)
        {
            using (var db = new CustDataContext()) 
         {
           var contact = db.CLIENT.Find(clientid);

             if (contact != null)
             {
                 return View(contact);
             }
             else
             {
                 return RedirectToAction("Index", "Customer");
             }

         }

Any help would be appreciated!

Best regards, Eero

4

2 に答える 2