DatabaseContext-->MvcWcfDbContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WcfServiceApplication.DatabaseContext
{
public class MvcWcfDbContext : DbContext
{
public MvcWcfDbContext()
: base("MvcWcfContext")
{
}
public DbSet<User> User { get; set; }
}
}
User.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace WcfServiceApplication
{
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
IService1.cs
using System.Collections.Generic;
using System.ServiceModel;
namespace WcfServiceApplication
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
IList<User> GetUsers();
[OperationContract]
User GetByUserId(int id);
[OperationContract]
void InsertUser(User user);
[OperationContract]
void UpdateUser(User user);
[OperationContract]
void DeleteUser(int id);
// TODO: Add your service operations here
}
}
Service1.svc.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WcfServiceApplication.DatabaseContext;
namespace WcfServiceApplication
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
private readonly MvcWcfDbContext _db=new MvcWcfDbContext();
public IList<User> GetUsers()
{
return _db.User.ToList();
}
public User GetByUserId(int id)
{
return _db.User.Find(id);
}
public void InsertUser(User user)
{
_db.User.Add(user);
_db.SaveChanges();
}
public void UpdateUser(User user)
{
_db.Entry(user).State=EntityState.Modified;
_db.SaveChanges();
}
public void DeleteUser(int id)
{
var user = _db.User.Find(id);
_db.User.Remove(user);
_db.SaveChanges();
}
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_db.Dispose();
}
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Mvc-WcfTrainingApplication-->WelcomeController.cs
using System.Web.Mvc;
using Mvc_WcfTrainingApplication.MvcWcfService;
using Users = Mvc_WcfTrainingApplication.MvcWcfService.User;
namespace Mvc_WcfTrainingApplication.Controllers
{
/// <summary>
///
/// </summary>
public class WelcomeController : Controller
{
//
// GET: /Welcome/
private readonly Service1Client _mvcWcfServiceClient = new Service1Client();
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult HomePageView()
{
return View("HomeView",_mvcWcfServiceClient.GetUsers());
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ActionResult Create()
{
return View();
}
/// <summary>
///
/// </summary>
/// <param name="users"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Create(Users users)
{
if (ModelState.IsValid)
{
_mvcWcfServiceClient.InsertUser(users);
return RedirectToAction("HomePageView");
}
return View("HomeView", _mvcWcfServiceClient.GetUsers());
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Edit(int id=0)
{
var users = _mvcWcfServiceClient.GetByUserId(id);
if (users == null)
{
return HttpNotFound();
}
return View("Edit",users);
}
/// <summary>
///
/// </summary>
/// <param name="users"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Edit(Users users)
{
if (ModelState.IsValid)
{
//var user = _mvcWcfServiceClient.GetByUserId(users);
_mvcWcfServiceClient.UpdateUser(users);
return RedirectToAction("HomePageView");
}
return View("HomeView", _mvcWcfServiceClient.GetUsers());
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Delete(int id = 0)
{
var users = _mvcWcfServiceClient.GetByUserId(id);
if (users == null)
{
return HttpNotFound();
}
return View("Delete", users);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
_mvcWcfServiceClient.DeleteUser(id);
return RedirectToAction("HomePageView");
}
}
}
HomeView.cshtml
@model Mvc_WcfTrainingApplication.MvcWcfService.User[]
@{
ViewBag.Title = "HomeView";
}
<h2>HomeView</h2>
@using (Html.BeginForm())
{
<table>
<thead>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td> </td>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<td>
@user.Id
</td>
<td>
@user.FirstName
</td>
<td>
@user.LastName
</td>
<td>
@Html.ActionLink("Edit", "Edit", "Welcome",new{id=user.Id},null)
</td>
<td>
@Html.ActionLink("Delete","Delete","Welcome",new{id=user.Id},null)
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td>
@Html.ActionLink("Create", "Create", "Welcome", null, null)
</td>
</tr>
</tfoot>
</table>
}
Create.cshtml
@model Mvc_WcfTrainingApplication.MvcWcfService.User
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.FirstName)
</div>
<div>
@Html.EditorFor(model => model.FirstName)
</div>
<div>
@Html.LabelFor(model => model.LastName)
</div>
<div>
@Html.EditorFor(model => model.LastName)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
<div>
@Html.ActionLink("Back to Home Page", "HomePageView")
</div>
Edit.cshtml
@model Mvc_WcfTrainingApplication.MvcWcfService.User
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.Id)
<div>
@Html.LabelFor(model => model.FirstName)
</div>
<div>
@Html.EditorFor(model => model.FirstName)
</div>
<div>
@Html.LabelFor(model => model.LastName)
</div>
<div>
@Html.EditorFor(model => model.LastName)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
<div>
@Html.ActionLink("Back to Home Page", "HomePageView")
</div>
Delete.cshtml
@model Mvc_WcfTrainingApplication.MvcWcfService.User
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div >
@Html.DisplayNameFor(model => model.FirstName)
</div>
<div >
@Html.DisplayFor(model => model.FirstName)
</div>
<div >
@Html.DisplayNameFor(model => model.LastName)
</div>
<div >
@Html.DisplayFor(model => model.LastName)
</div>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to Home Page", "HomePageView")
</p>
}
Also add connection string to Mvc_WcfTrainingApplication as same as WcfServiceApplication connection string.
Comments
Post a Comment