Hallo an die Experten!
Ich beschäftige mich gerade mit den EntityFramework 4.1 und dem T4 Template "ADO.NET DbContextGenerator". In der Datenbank habe ich eine Tabelle "Customers" mit einer 1:n-Beziehung auf die Tabelle "Projects".
Das T4-Template erzeugt mir für "Customer" folgenden Code:namespace Efx41
{
using System;
using System.Collections.Generic;
public partial class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
public int ID { get; set; }
public string CustomerName { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
} Nun nehme ich eine WindowsForms-Anwendung her mit einer BindingSource "SrcProjects", an die die Projects gebunden werden sollen. Die Projects lade ich über die Navigationproperty Customer.Projects mit folgenden Code:using (EfxTestSchemaEntities ctx = new EfxTestSchemaEntities())
{
//ProxyCreation aussschalten
((IObjectContextAdapter)ctx).ObjectContext.
ContextOptions.ProxyCreationEnabled = false;
var customer = ctx.Customers.First();
var projects = customer.Projects;
SrcProjects.DataSource = projects;
Debug.WriteLine("Projects Type Name: " + projects.GetType().FullName);
Debug.WriteLine("Projects is IListSource: " + (projects is _
IListSource).ToString());
Debug.WriteLine("BindingSrc List Type: " + SrcProjects.List.GetType( _
).FullName);
} Das Ergebnis der Debug.WriteLine-Zeilen ist:Projects Type Name: System.Collections.Generic.HashSet`1[[Efx41.Project, Efx41, _
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Projects is IListSource: False
BindingSrc List Type: _
System.ComponentModel.BindingList`1[[System.Collections.Generic.HashSet`1[[Ef_
x41.Project, Efx41, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], _
System.Core, Version=4.0.0.0, Culture=neutral, _
PublicKeyToken=b77a5c561934e089]] Was mich dabei wundert ist:
Woher kommt die BindingList<T> bei SrcProjects.List?
Die Collection, die von Customer.Projects zurückgegeben wird, ist ein HashSet<T>. HashSet<T> ist aber weder selbst eine IBindingList noch eine IListSource, die eine IBindingList in IListSource.GetList() zurückgeben könnte.
Woher nimmt sich die BindingSource dann jedoch die BindingList<T>???
Ratlos fragt
D.
Beitrag wurde zuletzt am 27.03.12 um 00:34:29 editiert. |