更新的类或视图模型集合 [英] Updating a collection of classes or viewmodel
问题描述
我有一个包含两个班一个ViewModel ...
公共类vwbooking
{
公众预订预订{搞定;组; }
公共IEnumerable的<跟踪和GT; {痕迹得到;组; }
}
预订和跟踪是实体的EDMX。
我想通过一个调用来更新这两个类中的数据进行保存。
这是我尝试过,连同其他几个不成功的射中最黑暗的变种...
公众的ActionResult编辑(vwbooking vwbooking)
{
如果(ModelState.IsValid)
{
DbEntityEntry< vwbooking>进入= db.Entry(vwbooking);
entry.Property(E => e.bookings.firstname).IsModified = TRUE; db.SaveChanges();
}
}
调用保存的方法时,我得到以下错误...
的实体类型vwbooking是不是该机型为当前上下文的一部分。
成功GET方法加载。该职位是我在遇到麻烦。这是GET方法...
公众的ActionResult编辑(INT ID = 0)
{
预订预订= db.bookings.Find(ID);
VAR视图模型=新vwbooking();
viewModel.bookings =预订;
viewModel.traces =(从升的db.traces那里l.bookingid == booking.bookingid选择L);
返回视图(视图模型);
}
这是我的数据库上下文类
公共类salesContext:的DbContext
{
公共salesContext():基地()
{
Configuration.LazyLoadingEnabled = TRUE;
} 公共salesContext(字符串连接):基地(连接)
{
Configuration.LazyLoadingEnabled = TRUE;
} 公共DbSet<预订>预订{搞定;组; }
公共DbSet<跟踪和GT; {痕迹得到;组; } 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
base.OnModelCreating(模型构建器);
modelBuilder.Entity<预订>()HasKey。(E => e.bookingid);
modelBuilder.Entity<跟踪和GT;()HasKey。(E => e.traceid);
}
}
在code为更新你的模型是:
db.Attach(vwbooking.bookings)
db.ObjectStateManager.ChangeObjectState(vwbooking.bookings,System.Data.EntityState.Modified)vwbooking.traces.ToList()的ForEach(
T =>
{
db.Attach(T);
db.ObjectStateManager.ChangeObjectState(T,System.Data.EntityState.Modified);
}
);db.SaveChanges();
尝试在编辑器这个code
I have a viewModel that contains two classes...
public class vwbooking
{
public booking bookings { get; set; }
public IEnumerable<trace> traces { get; set; }
}
Booking and trace are entities in an edmx.
I want to update the data in these two class with one call to save.
This is what I've tried, along with several other unsuccessful "shot-in-the-dark" variants...
public ActionResult Edit(vwbooking vwbooking)
{
if (ModelState.IsValid)
{
DbEntityEntry<vwbooking> entry = db.Entry(vwbooking);
entry.Property(e => e.bookings.firstname).IsModified = true;
db.SaveChanges();
}
}
I get the following error when calling the save method...
The entity type vwbooking is not part of the model for the current context.
The GET method loads successfully. The post is where I'm having trouble. This is the GET method...
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);
}
This is my db context class
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);
}
}
The code for update your model is:
db.Attach(vwbooking.bookings)
db.ObjectStateManager.ChangeObjectState(vwbooking.bookings, System.Data.EntityState.Modified)
vwbooking.traces.ToList().ForEach(
t =>
{
db.Attach(t);
db.ObjectStateManager.ChangeObjectState(t, System.Data.EntityState.Modified);
}
);
db.SaveChanges();
try this code in the Edit Controller
这篇关于更新的类或视图模型集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!