Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架7和绑定列表_C#_Winforms_Entity Framework_Bindinglist - Fatal编程技术网

C# 实体框架7和绑定列表

C# 实体框架7和绑定列表,c#,winforms,entity-framework,bindinglist,C#,Winforms,Entity Framework,Bindinglist,我正在尝试将EF7与Winforms一起使用,我希望使用BindingList来支持双向绑定 在EF6中,我可以通过调用.ToBindingList _context = new ProductContext(); _context.Categories.Load(); this.categoryBindingSource.DataSource = _context.Categories.Local.ToBindingList(); 然而在EF7中,既没有Local属性,也没有ToBindi

我正在尝试将EF7与Winforms一起使用,我希望使用BindingList来支持双向绑定

在EF6中,我可以通过调用
.ToBindingList

_context = new ProductContext();
_context.Categories.Load(); 
this.categoryBindingSource.DataSource =
_context.Categories.Local.ToBindingList();
然而在EF7中,既没有
Local
属性,也没有
ToBindingList


如何使用EF7在Winforms上实现双向绑定?

您永远不应该绑定,尤其不应该直接双向绑定到EF对象。这意味着您将在应用程序的生命周期中使用DbContext,这将随着时间的推移给您带来麻烦

使用EF对象在大多数情况下没有意义,因为您几乎永远不会向用户显示确切的数据库对象。在大多数情况下,视图将显示多个数据对象的组合

因此,不要直接使用对象,而是创建定制的viewmodels以向用户显示,您可以从新的DbContext获取这些ViewModel。这些视图模型可以实现
INotifyPropertyChanged

完成抓取后,请处置DbContext,并在用户希望保存任何更改时创建一个新的DbContext

下面是一个简单的例子:

public class UserViewModel : INotifyPropertyChanged
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public int GroupId { get; set; }
    public string GroupName { get; set; }

    // INotifyPropertyChanged implementation or use Fody
}

public class UserModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual GroupModel Group { get; set; }
}

public class GroupModel
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual HashSet<UserModel> Users { get; set; } 
}

public class UserRepository
{
    public IEnumerable<UserViewModel> GetUsers()
    {
        using (var db = new YourDbContext())
        {
            return (
                from user in db.Users
                select new UserViewModel
                {
                    UserId = user.Id,
                    UserName = user.Name,
                    GroupId = user.Group.Id,
                    GroupName = user.Group.Name,
                }).ToArray();
        }
    }
    public void SaveChanges(UserViewModel user)
    {
        using (var db = new YourDbContext())
        {
            // get user
            // modify and...
            db.SaveChanges();
        }
    }
} 
公共类UserViewModel:INotifyPropertyChanged
{
public int UserId{get;set;}
公共字符串用户名{get;set;}
public int GroupId{get;set;}
公共字符串组名{get;set;}
//INotifyPropertyChanged实现或使用Fody
}
公共类用户模型
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟GroupModel组{get;set;}
}
公共类组模型
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟哈希集用户{get;set;}
}
公共类用户存储库
{
公共IEnumerable GetUsers()
{
使用(var db=new YourDbContext())
{
返回(
来自db.Users中的用户
选择新的UserViewModel
{
UserId=user.Id,
用户名=user.Name,
GroupId=user.Group.Id,
GroupName=user.Group.Name,
}).ToArray();
}
}
public void SaveChanges(UserViewModel用户)
{
使用(var db=new YourDbContext())
{
//获取用户
//修改并。。。
db.SaveChanges();
}
}
} 

结果可以很容易地包装在一个

中。那么,您是否尝试过手动创建一个包含所有项目的绑定列表,订阅
ListChanged
事件,并将更改反映回DbContext?我不知道实体框架代理是否实现了
INotifyPropertyChanged
。如果他们这样做了,那应该是直截了当的。非常感谢你的帮助。