Domanda

I read the following article about EF5 Databinding with WinForms.

I use VS2010, .NET 4.0 and EF5 Model First approach, and I don't know how to change navigation property return type in VS2010 EF Designer, without changing auto-generated entity class. For example I need to change navigation property return type from ICollection<T> to ObservableCollection<T>, but in Properties View there is 'Return type' option disabled.

È stato utile?

Soluzione

I found a solution. It is possible to change type of all navigation properties which are collections. Text Template *.tt file for Model First auto-generated entities should be edited.

By default, EF5 generates navigation properties of HashSet type:

If(edmProperty.ToEndMember.RelationshipMultiplicity = RelationshipMultiplicity.Many)
     defaultValue = " = New HashSet(Of " & propertyType & ")"
     propertyType = "ICollection(Of " & propertyType & ")"
End If

To change navigation properties type to ObservableCollection<T>, two lines of *.tt code should be modified:

If(edmProperty.ToEndMember.RelationshipMultiplicity = RelationshipMultiplicity.Many)
     defaultValue = " = New ObservableCollection(Of " & propertyType & ")"
     propertyType = "ObservableCollection(Of " & propertyType & ")"
End If

Afterwards, entity classes should be regenerated automatically.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top