gpt4 book ai didi

wpf - EF Code First 绑定(bind)到列表框

转载 作者:行者123 更新时间:2023-12-03 10:21:08 25 4
gpt4 key购买 nike

如果有人能告诉我如何将列表框绑定(bind)到 Entity Framework (EF 代码优先),我将不胜感激。

我正在使用棱镜和mef。有两种 View ,一种带有按钮,另一种带有列表框。 (每个都在自己的棱镜区域)。
通过单击按钮,我创建了一个新的 Employee 对象并通过 Entity Framework 将其插入到数据库中。

问题是我的列表框没有向我显示列表中新添加的 Employee 对象。应该改变什么才能使这项工作?

部分代码:

主视图:

    <Grid>
<StackPanel>
<TextBlock Text="Hello From MainView (Core Module)" />
<ListBox ItemsSource="{Binding Employees, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>

主视图型号:
namespace EmployeeViewer.Core.ViewModels
{
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MainViewModel : NotificationObject
{
private IUnitOfWork _UnitOfWork;

[ImportingConstructor]
public MainViewModel(IUnitOfWork unitOfWork) // IEventAggregator eventAggregator
{
this._UnitOfWork = unitOfWork;

Init();
}

private void Init()
{
this.Employees = new ObservableCollection<Employee>(_UnitOfWork.EmployeeRepository.Get());
//this.Employees = new BindingList<Employee>(_UnitOfWork.EmployeeRepository.Get().ToList());
}

public ObservableCollection<Employee> Employees { get; set; }
//public BindingList<Employee> Employees { get; set; }

}
}

从 GenericRepository 获取方法:
    public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = _DbSet;

if (filter != null)
{
query = query.Where(filter);
}

foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}

if (orderBy != null)
{
return orderBy(query).AsEnumerable();//.ToList();
}
else
{
return query.AsEnumerable();//.ToList();
}
}

通过另一个区域和 View 中的 ICommand(实现为 RelayCommand),我首先创建一个新的 Employee 对象并将其插入到 Entity Framework 代码中。
public ICommand AddEmployeeCommand
{
get { return _AddEmployeeCommand; }
}

private void AddEmployee()
{
_UnitOfWork.EmployeeRepository.Insert(new Employee() { FirstName = "Test", LastName = "Via cmd" });
_UnitOfWork.Save();
}

最佳答案

搜索后Bing.com我发现 EF 有一个 本地 属性(property)。

如果您正在寻找解决方案,这就是我所做的:

我向我的 GenericRepository 添加了一个新方法:

public virtual ObservableCollection<TEntity> GetSyncedEntityItems()
{
return _DbSet.Local;
}

这是最重要的部分:.Local 为您提供与 EF! 同步的 ObservableCollection。

在我的 ViewModel 中,我这样做:
this.Employees = _UnitOfWork.EmployeeRepository.GetSyncedEntityItems();

并且向 EF 添加新员工将导致我的其他 View 更新:
private void AddEmployee()
{
_UnitOfWork.EmployeeRepository.Insert(new Employee() { FirstName = "Test", LastName = "Via cmd" });
_UnitOfWork.Save();
}

我不知道这是否是最好的解决方案,如果有更好的选择,请告诉我!

关于wpf - EF Code First 绑定(bind)到列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7528400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com