【发布时间】:2013-10-17 12:58:11
【问题描述】:
我有一个包含两个类的 viewModel...
public class vwbooking
{
public booking bookings { get; set; }
public IEnumerable<trace> traces { get; set; }
}
预订和跟踪是 edmx 中的实体。
我想通过一次调用来更新这两个类中的数据。
这是我尝试过的,以及其他几个不成功的“暗中射击”变体......
public ActionResult Edit(vwbooking vwbooking)
{
if (ModelState.IsValid)
{
DbEntityEntry<vwbooking> entry = db.Entry(vwbooking);
entry.Property(e => e.bookings.firstname).IsModified = true;
db.SaveChanges();
}
}
调用保存方法时出现以下错误...
实体类型 vwbooking 不是当前上下文模型的一部分。
GET 方法加载成功。帖子是我遇到麻烦的地方。这是GET方法...
public ActionResult Edit(int id = 0)
{
booking booking = db.bookings.Find(id);
var viewModel = new vwbooking();
viewModel.bookings = booking;
viewModel.traces = (from l in db.traces where l.bookingid == booking.bookingid select l);
return View(viewModel);
}
这是我的数据库上下文类
public class salesContext : DbContext
{
public salesContext() : base()
{
Configuration.LazyLoadingEnabled = true;
}
public salesContext(string Connection) : base(Connection)
{
Configuration.LazyLoadingEnabled = true;
}
public DbSet<booking> bookings { get; set; }
public DbSet<trace> traces { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<booking>().HasKey(e => e.bookingid);
modelBuilder.Entity<trace>().HasKey(e => e.traceid);
}
}
【问题讨论】:
-
这是因为 vwbooking 不是 DbContext 中的模型。它只是保存数据以供您的视图显示。您必须更新 vwbooking 属性,因为它们构成了您的模型。
-
好的 - 我想我明白了,但我认为这就是我在做什么?我应该做些什么不同的事情?
-
我在下面的回答中添加了更多详细信息
标签: c# asp.net-mvc entity-framework viewmodel