我正在尝试使用Entity Framework将我的MVC项目与数据库链接,但我无法弄清楚如何将新记录存储到数据库中。到目前为止,我有以下代码:
//model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MvcApplication1.Controllers;
namespace MvcApplication1.Models
{
public class CarUser
{
public int ID { get; set; }
[DisplayName ("First Name")]
public string Fname { get; set; }
[DisplayName ("Surname")]
public string Sname { get; set; }
}
}
//controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class CarUserController : Controller
{
private FormDataContext db = new FormDataContext();
//
// GET: /CarUser/
public ActionResult Index()
{
return View(db.CarUsers.ToList());
}
//
// GET: /CarUser/Details/5
public ActionResult Details(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// GET: /CarUser/Create
public ActionResult Create()
{
return View();
}
//
// POST: /CarUser/Create
[HttpPost]
public ActionResult Create(CarUser caruser)
{
if (ModelState.IsValid)
{
db.CarUsers.Add(caruser);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(caruser);
}
//
// GET: /CarUser/Edit/5
public ActionResult Edit(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// POST: /CarUser/Edit/5
[HttpPost]
public ActionResult Edit(CarUser caruser)
{
if (ModelState.IsValid)
{
db.Entry(caruser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(caruser);
}
//
// GET: /CarUser/Delete/5
public ActionResult Delete(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// POST: /CarUser/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
CarUser caruser = db.CarUsers.Find(id);
db.CarUsers.Remove(caruser);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
//formdatacontext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcApplication1.Models
{
public class FormDataContext : DbContext
{
public DbSet<CarUser> CarUsers
{ get; set; }
}
}
//formdatainitialiser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
namespace MvcApplication1.Models
{
public class FormDataInitializer : DropCreateDatabaseAlways<FormDataContext>
{
protected override void Seed(FormDataContext context)
{
base.Seed(context);
var CarUsers = new List<CarUser>
{
new CarUser {
ID = 1,
Fname = "Craig",
Sname = "Higginson",
}
};
CarUsers.ForEach(s => context.CarUsers.Add(s));
context.SaveChanges();
}
}
}
我的web.config文件中还有以下内容
<connectionStrings>
<add name="FormDataContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=FormData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
这一切都很好,当我导航到/ caruser时,我能够创建新的记录。这些记录未存储在我的数据库中。
我是否需要首先在数据库中创建表,或者如果我已正确引用数据库,Entity Framework是否应该为我创建此表?
答案 0 :(得分:1)
如果您使用Code First方法,那么Entity Framework将为您构建表。看起来您没有使用Code First,因此您将在数据库中创建表。我假设您正在使用实体数据模型(.edmx)?如果是这样,您将在数据库中创建表,然后更新数据模型(.edmx)。如果您尚未创建.edmx文件,则需要这样做 - .edmx文件将包含您的所有CRUD操作。
我感到困惑的是,如果表格不存在(例如,数据模型所代表的表格没有映射到数据库中的实际表格,那么您的代码会抛出错误,因为它不存在)。所以,问题是,你的桌子已经存在了吗?如果是,则逐行逐步执行代码以找出未保存记录的原因。如果它不存在,则通过SQL Server Management Studio(或类似)添加表,然后打开.edmx文件,右键单击出现的布局,单击“从数据库更新模型”。
答案 1 :(得分:1)
答案 2 :(得分:0)
edmx
项目添加到现有解决方案中,该解决方案是db的模型,包含所有表和SP。DataAccessLayer.cs
。DataAccessLayer.cs
中,添加对edmx
项目的引用。使用此代码:
using (var entity = new FormDataContext())
{
//entity. will give you all the tables in the database.
}