Connectivity Asp.Net MVC 4.0 with Entity Frame Work without edmx file
-- Execute this query for Sql Server
use TestDb
create table Student(SrNo int primary key,Name nvarchar(50),Course nvarchar(50),Age int not null,MobileNo nvarchar(50))
First Step- Open .Net
IDE and following steps are given
bellow……..
1.
File->New->Project
2.
How
to create new MVC Project ………………………………………….
3. Select basic from here if you select empty then you didn’t find some
specific namespace in your MVCApplication like:
using System.Data.Entity;
and you’ll not find Scripts file for JQUERY
4. How to add class for database connectivity
5. Because you
are not using Model1.edmx in your MVCAppliction that’s why create class in Models which name is MyClass.cs
Write this code
in MyClass.cs ………………………………………….
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
//using this namespace................
using
System.ComponentModel.DataAnnotations;
using
System.ComponentModel.DataAnnotations.Schema;
namespace
MvcApplication5.Models
{
[Table("Student")]
public class MyClass
{
// Field SrNo is primary
key in table
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
public int SrNo { get; set; }
public string Name { get; set; }
public string Course { get; set; }
//because Age Field is not
null in sql server
public int? Age { get; set; }
public string MobileNo { get; set; }
}
}
Write this code in database.cs ………………………………………….
Again we create class in Models which name is database.cs
Write this code in database.cs ………………………………………….
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
//this namespace for
DbContext
using System.Data.Entity;
namespace MvcApplication.Models
{
public class database:DbContext
{
// this MyConnection is name of
ConnectionString which exist in Web.cnfig file
public database()
: base("MyConnection")
{
}
public DbSet<MyClass> myclass { get; set; }
}
}
6.
Now the point is how to modify your connection
string in Web.config file because you are not using Model1.edmx
Note: when you add View with Controller and select
strongly type checkbox then you will not find textbox and label for
primary key data field so please
add manually code for primary key field data in your Index.cshtml as shown below code for SrNo…………..
3. Code for
Index.cshtml…………………………..
@model MvcApplication5.Models.MyClass
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>MyClass</legend>
<div class="editor-label">
@Html.LabelFor(model
=> model.SrNo)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.SrNo)
@Html.ValidationMessageFor(model
=> model.SrNo)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Name)
@Html.ValidationMessageFor(model
=> model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Course)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Course)
@Html.ValidationMessageFor(model
=> model.Course)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Age)
@Html.ValidationMessageFor(model
=> model.Age)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.MobileNo)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.MobileNo)
@Html.ValidationMessageFor(model
=> model.MobileNo)
</div>
<p>
<input type="submit" value="Create" />
</p>
<div style="color:red;"> @ViewData["error"]</div>
<div style="color:green;"> @ViewData["message"]</div>
</fieldset>
}
<div>
@Html.ActionLink("Back to
List",
"Index")
</div>
</body>
</html>
4. Source code of
HomeController.cs
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using this namespace
.......................
using
MvcApplication5.Models;
namespace
MvcApplication5.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
database dd = new database();
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyClass mm)
{
try
{
dd.myclass.Add(mm);
dd.SaveChanges();
ViewData["message"] = "Save
Successfully";
}
catch (Exception ex)
{
ViewData["error"] = ex.Message;
}
return View();
}
}
}
Big thanks for imparting such knowledge
ReplyDelete