简体   繁体   English

尝试将Datagridview与实体框架绑定时出错?

[英]Error trying to bind Datagridview with Entity Framework?

Model: 模型:

Code: 码:

  public partial class frmCotizaciones : Form
{
    public frmCotizaciones()
    {
        InitializeComponent();
        using (var ctx = new AefesaEntities1())
        {
            dataGridView1.DataSource = ctx.Productos.ToList();
        }
    }
}

Error: 错误:

I'm new to Entity Framework, and I want to know how to use it, since I can see the advantages of using it. 我是Entity Framework的新手,我想知道如何使用它,因为我可以看到使用它的优点。 What I'm trying to do is simply bind the Productos dataset to the datagrid, but it throws that exception, I'd really appreciate your help. 我想要做的只是将Productos数据集绑定到datagrid,但它会抛出异常,我真的很感谢你的帮助。

The problem is that your Producto class has not only simple properties ( string , int ) but also navigation properties that point to other classes the producto is in relation. 问题是您的Producto类不仅具有简单属性( stringint ),还具有指向producto所关联的其他类的导航属性。

In this particular case the error shows that the producto has a list of DetalleCotizacione (whatever it is). 在这种特殊情况下,错误显示producto有一个DetalleCotizacione列表(无论它是什么)。 The proxy class for the producto returned from EF has a lazy loaded property for this, which means that the property is not evaluated until some code asks for it. 从EF返回的producto的代理类具有延迟加载的属性,这意味着在某些代码要求之前不会评估该属性。

And now comes the tricky part. 现在是棘手的部分。 Your client code asks for a list 您的客户端代码要求提供列表

ctx.Productos.ToList();

The list will be populated with productos where all simple properties ( string , int ) are initialized and all lazy loaded properties are not. 该列表将填充productos,其中所有简单属性( stringint )都已初始化,并且所有延迟加载属性都不是。

And then you dispose the data context. 然后你处理数据上下文。

Now the data grid binder inspects (by reflection) your objects and find a lot of public properties, including these that are initialized ( string , int ) and these that are not - navigation properties. 现在数据网格绑定器检查(通过反射)您的对象并找到许多公共属性,包括初始化的那些( stringint )和不属于导航属性的属性。 The grid creates columns for all your public properties and starts to populate rows. 网格为所有公共属性创建列,并开始填充行。 It then asks for values of all public fields of the producto and in case of navigation propeties - it fails because your context is already disposed (at the end of the using block). 然后它会询问 producto的所有公共字段的 ,并且在导航属性的情况下 - 它会失败,因为您的上下文已经被处理 (在using块的末尾)。

There are two workarounds: 有两种解决方法:

  1. initialize the list of columns of your grid in an explicit way rather than relying on auto generation of columns. 以显式方式初始化网格列的列表,而不是依赖于自动生成列。 This way you could create columns for all properties that are not lazy loaded navigation properties. 这样,您可以为不是延迟加载的导航属性的所有属性创建列。

  2. project your productos to whatever anonymous type you want thus making explicit loading of simple and navigation properties inside the block where context lives: 将您的productos投射到您想要的任何匿名类型,从而在上下文所在的块内显式加载简单和导航属性:

     using (var ctx = new AefesaEntities1()) { dataGridView1.DataSource = ctx.Productos .Select( p => new { id = p.ID, whatever = p.Whatever, lazy = p.Lazy1, etc. } .ToList(); } 

you're going to get all members of entity "Productos" including "Cotizaciones" and "Detallecotizacionnes". 你将获得实体“Productos”的所有成员,包括“Cotizaciones”和“Detallecotizacionnes”。 try to specify the members to be assigned to your datagrid columns. 尝试指定要分配给datagrid列的成员。

example : 例如:

using (var ctx = new AefesaEntities1())
        {
           dataGridView1.DataSource = ctx.Productos.select(p=>new{p.IdProducto ,p.Descripcion ,p.PrecioActual}).ToList();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM