我正在尝试创建一个Web应用程序,但发现EntityCommandExecutionException

时间:2014-06-23 12:39:36

标签: c# visual-studio-2010 asp.net-mvc-3

我正在尝试创建一个Web应用程序,但发现EntityCommandExecutionException,这是我的Controller类

namespace DemoMVC.Controllers
{
    public class ContactController : Controller
    {
            ContactContext db = new ContactContext();
            public ActionResult Index()
        {
            var contact = db.Contacts.ToList();
            return View(contact);
        }
        public ActionResult Details(int id)
        {
            return View();
        }
        public ActionResult Create()
        {
           return View();
        } 

        [HttpPost]
        public ActionResult Create(Contact person)
        {
            try
            {
                // TODO: Add insert logic here
                db.Contacts.Add(person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Contact/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Contact/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Contact/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Contact/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

这是我的连接字符串

<add name="ContactContext" connectionString="Data Source=RMSIPDB;Initial Catalog=CoroporateApps;User ID=CorApps;Password=CorApps_123" 
providerName="System.Data.SqlClient" />

And this is my View Page 
@model IEnumerable<DemoMVC.Models.Contact>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Name
        </th>
        <th>
            Address
        </th>
        <th>
            Mobile
        </th>
        <th>
            DataDate
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Mobile)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DataDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table> 

当我执行代码时,我得到了EntityCommandExecutionException,任何人都可以帮助我。

0 个答案:

没有答案