问题
I want to create a set of DataGridView controls by dragging their respective DataSources onto a form. I would do that for an object and one of its navigation properties (from the DataSources), thus having two grids in a Master-Detail relationship.
After moving to Visual Studio 2012 (and EF 5.0), the first grid comes through as expected (with the navigator). However, for the "detail" grid, only two columns appear ("Count" & "IsReadOnly"). I've tried it with other tables/objects and I hit the same problem. I've tried "Edit Columns" & "Add Column" to no available. I use northwind as an example. I want 2 datagridview in a form. one for a customer and another for related orders. a typical master/details view.
I added a model to project (edmx) file.
added a new datasource to model. two objects. customer and orders tables!
dragged customer entity as datagridview. All fields (properties) are in grid.
4 when dragging the orders from customer entity not order entity itself, there are only two column named "count" and "readonly"
Above procedures work fine in linq to sql VS 2012, EF 4.0, .NET 4.5. But NOT for EF 5.0.
and,
The steps are exactly same as the way beth massi described in http://blogs.msdn.com/b/bethmassi/archive/2008/12/10/master-details-with-entity-framework-explicit-load.aspx However this is not an issue of lazy or eager loading!
回答1:
In the class for the entity you are trying to work with change the navigation property type to ObservableCollection.
From this:
public Customer()
{
this.CustomerAddresses = new HashSet<CustomerAddress>();
}
public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}
To this:
public Customer()
{
this.CustomerAddresses = new ObservableCollection<CustomerAddress>();
}
public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}
Since this is auto generated code you will also need to change some rules in your code generation file. Try manually making these changes to ensure they work first then make the changes. For me I had to make the following changes to my .tt file
Add using System.Collections.ObjectModel by adding the line below that mentions that.
return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
? string.Format(
CultureInfo.InvariantCulture,
"{0}using System;{1}" +
"{2}",
inHeader ? Environment.NewLine : "",
includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
includeCollections ? (Environment.NewLine + "using System.Collections.ObjectModel;") : "",
inHeader ? "" : Environment.NewLine)
: "";
And change your HastSet declaration to Observable Collection
this.<#=code.Escape(navigationProperty)#> = new ObservableCollection<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
Also, change ICollection here to be ObservableCollection
navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
Your code generation file may differ from mine but these code parts should give you an idea of what to search for in your file.
来源:https://stackoverflow.com/questions/12695754/unable-to-create-gridview-by-dragging-object-datasource-to-winform