我正在努力更新数据库中现有讲师的数据。
每个讲师都有他/她教授的Name、AcademicDegree和Courses ( Courses==Lessons)。
实际上有更多的属性,class Lecturer
但它们不相关。为简单起见,我们假设POCO类是这样定义的:
// POCO class (Entity Framework Reverse Engineering Code First)
public class Lecturer
{
public Lecturer()
{
this.Courses = new List<Course>();
}
public int Id_Lecturer { get; set; } // Primary Key
public string Name { get; set; }
public int? Academic_Degree_Id { get; set; }
public virtual AcademicDegree AcademicDegree { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
在数据访问层中,我有方法应该更新等于(使用,和)void UpdateLecturer(Lecturer lecturer)
的讲师。Id_Lecturer
lecturer.Id_Lecturer
lecturer.Name
lecturer.AcademicDegree
lecturer.Courses
这是非常方便的方法,因为在ViewModel中我可以调用_dataAccess.UpdateLecturer(SelectedLecturer)
(在XAMLSelectedLecturer
中绑定的位置;属性可以由用户在es 和中设置)。SelectedLecturer
TextBox
Checkbox
不幸的是这种方法:
public void UpdateLecturer(Lecturer lecturer)
{
using(var db = new AcademicTimetableDbContext())
{
// Find lecturer with Primary Key set to 'lecturer.Id_Lecturer':
var lect = db.Lecturers.Find(lecturer.Id_Lecturer);
// If not found:
if (lect == null)
return;
// Copy all possible properties:
db.Entry(lect).CurrentValues.SetValues(lecturer);
// Everything was copied except 'Courses'. Why?!
// I tried to add this, but it doesn't help:
// var stateMgr = (db as IObjectContextAdapter).ObjectContext.ObjectStateManager;
// var stateEntry = stateMgr.GetObjectStateEntry(lect);
// stateEntry.SetModified();
db.SaveChanges();
}
}
更新所有内容(即Id_Lecturer
、和)Name
,但 .之后不变的除外。Academic_Degree_Id
AcademicDegree
Courses
db.SaveChanges()
为什么?我该如何解决?
类似的问题:
- 更新实体框架中的实体
- 如何-我-附加-实体-框架-对象-从-数据库
- using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx
- 实体框架更新与相关实体
- 实体框架代码首先不分离方法在 dbcontext
- 编辑 -
我也尝试过这种方式(想法来自这篇文章):
public void UpdateLecturer(Lecturer lecturer)
{
using (var db = new AcademicTimetableDbContext())
{
if (lecturer == null)
return;
DbEntityEntry<Lecturer> entry = db.Entry(lecturer);
if (entry.State == EntityState.Detached)
{
Lecturer attachedEntity = db.Set<Lecturer>().Find(lecturer.Id_Lecturer);
if (attachedEntity == null)
entry.State = EntityState.Modified;
else
db.Entry(attachedEntity).CurrentValues.SetValues(lecturer);
}
db.SaveChanges();
}
}
但课程仍然不会覆盖旧值。
-- 编辑 2 --
作为对@Slauma 问题的回应,我将描述我是如何加载的(作为参数SelectedLecturer
传递给方法)。UpdateLecturer(Lecturer lecturer)
我正在实施MVVM概念,因此我的解决方案中有View项目,并设置为. 在视图中有从数据库中获取的所有讲师的列表。以这种方式绑定:DataContext
LecturerListViewModel
DataGrid
DataGrid
<DataGrid AutoGenerateColumns="False" Name="LecturersDataGrid" HeadersVisibility="Column" IsReadOnly="True" ItemsSource="{Binding Lecturers,Mode=TwoWay}" SelectedItem="{Binding SelectedLecturer, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Academic degree" Binding="{Binding AcademicDegree.Degree_Name}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Click="EditButtonClick"/>
<Button Content="Delete" Command="{Binding DataContext.RemoveLecturer, ElementName=LecturersDataGrid}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Lecturers
LecturerListViewModel
以这种方式从构造函数中的数据库中获取:
///
/// Code within LecturerListViewModel class:
///
// All lecturers from database.
public ObservableCollection<Lecturer> Lecturers
// Constructor
public LecturerListViewModel()
{
// Call to Data Access Layer:
Lecturers = new ObservableCollection<Lecturer>(_dataAccess.GetAllLecturers());
// Some other stuff here...
}
private Lecturer _selectedLecturer;
// Currently selected row with lecturer.
public Lecturer SelectedLecturer
{
get { return _selectedLecturer; }
set { SetProperty(out _selectedLecturer, value, x => x.SelectedLecturer); }
}
///
/// Data Access Layer (within DataAccess class):
///
public IEnumerable<Lecturer> GetAllLecturers()
{
using (var dbb = new AcademicTimetableDbContext())
{
var query = from b
in dbb.Lecturers.Include(l => l.AcademicDegree).Include(l => l.Timetables).Include(l => l.Courses)
select b;
return query.ToList();
}
}