Abend,
ich bin gerade dabei einen meiner Anfängerfehler mit EF zu korrigieren. Ich hatte zu Anfang den bequemen Weg des Model First gewählt => Problem mit Migration usw. darum will ich jetzt mein Datenmodell auf CodeFirst umstellen.
Soweit so gut ich habe alle Entitäten beisamen und auch im Modell verlinkt aber es wird immer ein Fehler vom Typ System.Data.Entity.ModelConfiguration.ModelValidationException aufgeworfen mit folgendem Inhalt:
Das ich alle Keys mit Data Annotations eingebaut habe ist geprüft, das alle zusätzlichen Routinen in den Klassen die nicht gemappt werden sollen mit <NotMapped> markiert sind habe ich auch überprüft.
Die Entitäten "ListCollectionView", "cultureinfo" usw. sind keine Entitäten die als Klassen vorliegen, von den oben angeführten Typen wird in manchen Klassen nur die Listcollectionview genutzt => <NotMapped>
Als Beispiel eine Klasse die als Entität im Modell vorliegt:
Im Modell schaut es so aus:
(Modell ist stark verkürzt wegen Übersichtlichkeit)
Als absolute Verzweiflungstat habe ich jetzt das gesamte Modell in ein neues Projekt übertragen und dort funktioniert es komischerweise....
Hat jemand von euch einen Tipp für mich?
Danke
ich bin gerade dabei einen meiner Anfängerfehler mit EF zu korrigieren. Ich hatte zu Anfang den bequemen Weg des Model First gewählt => Problem mit Migration usw. darum will ich jetzt mein Datenmodell auf CodeFirst umstellen.
Soweit so gut ich habe alle Entitäten beisamen und auch im Modell verlinkt aber es wird immer ein Fehler vom Typ System.Data.Entity.ModelConfiguration.ModelValidationException aufgeworfen mit folgendem Inhalt:
VB.NET-Quellcode
- System.Data.Entity.ModelConfiguration.ModelValidationException occurred
- HResult=-2146233088
- Message=One or more validation errors were detected during model generation:
- HHHB2.ListCollectionView: : EntityType 'ListCollectionView' has no key defined. Define the key for this EntityType.
- HHHB2.CultureInfo: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
- HHHB2.DateTimeFormatInfo: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
- HHHB2.GroupDescription: : EntityType 'GroupDescription' has no key defined. Define the key for this EntityType.
- HHHB2.ItemPropertyInfo: : EntityType 'ItemPropertyInfo' has no key defined. Define the key for this EntityType.
- ListCollectionViews: EntityType: EntitySet 'ListCollectionViews' is based on type 'ListCollectionView' that has no keys defined.
- CultureInfoes: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
- DateTimeFormatInfoes: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.
- GroupDescriptions: EntityType: EntitySet 'GroupDescriptions' is based on type 'GroupDescription' that has no keys defined.
- ItemPropertyInfoes: EntityType: EntitySet 'ItemPropertyInfoes' is based on type 'ItemPropertyInfo' that has no keys defined.
- Source=EntityFramework
- StackTrace:
- bei System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
- bei System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
- bei System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
- bei System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
- bei System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
- bei System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
- bei System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext()
- bei System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
- bei System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
- bei System.Data.Entity.Internal.InternalContext.GetStateEntries()
- bei System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
- bei System.Data.Entity.DbContext.GetValidationErrors()
- bei HHHB2.MainModel.InitMainmodel() in E:\Entwicklung\HHHB2\HHHB2\Viewmodel\MainModel.vb:Zeile 64.
- InnerException:
Das ich alle Keys mit Data Annotations eingebaut habe ist geprüft, das alle zusätzlichen Routinen in den Klassen die nicht gemappt werden sollen mit <NotMapped> markiert sind habe ich auch überprüft.
Die Entitäten "ListCollectionView", "cultureinfo" usw. sind keine Entitäten die als Klassen vorliegen, von den oben angeführten Typen wird in manchen Klassen nur die Listcollectionview genutzt => <NotMapped>
Als Beispiel eine Klasse die als Entität im Modell vorliegt:
VB.NET-Quellcode
- Imports System.ComponentModel.DataAnnotations
- Imports System.Collections.ObjectModel
- Imports System.ComponentModel.DataAnnotations.Schema
- Public Class category_v01
- Inherits model_base
- 'Interne Variablen
- Dim v_cat_id As Integer
- Dim v_bezeichnung As String
- Dim v_color As String = "#000000"
- Dim v_symbol As Byte()
- Dim v_aktiv As Nullable(Of Boolean) = True
- Dim v_parent_cat_id As Nullable(Of Integer)
- <Key>
- Public Property cat_id As Integer
- Get
- Return v_cat_id
- End Get
- Set(value As Integer)
- If v_cat_id <> value Then
- v_cat_id = value
- NotifyPropertyChanged("cat_id")
- End If
- End Set
- End Property
- Public Property bezeichnung As String
- Get
- Return v_bezeichnung
- End Get
- Set(value As String)
- If v_bezeichnung <> value Then
- v_bezeichnung = value
- NotifyPropertyChanged("bezeichnung")
- End If
- End Set
- End Property
- Public Property color As String
- Get
- Return v_color
- End Get
- Set(value As String)
- If v_color <> value Then
- v_color = value
- NotifyPropertyChanged("color")
- End If
- End Set
- End Property
- Public Property symbol As Byte()
- Get
- Return v_symbol
- End Get
- Set(value As Byte())
- If v_symbol IsNot value Then
- v_symbol = value
- NotifyPropertyChanged("symbol")
- NotifyPropertyChanged("symbol_ever")
- End If
- End Set
- End Property
- Public Property aktiv As Nullable(Of Boolean)
- Get
- Return v_aktiv
- End Get
- Set(value As Nullable(Of Boolean))
- If v_aktiv <> value Then
- v_aktiv = value
- NotifyPropertyChanged("aktiv")
- End If
- End Set
- End Property
- Public Property parent_cat_id As Nullable(Of Integer)
- Get
- Return v_parent_cat_id
- End Get
- Set(value As Nullable(Of Integer))
- If v_parent_cat_id <> value Then
- v_parent_cat_id = value
- NotifyPropertyChanged("parent_cat_id")
- End If
- End Set
- End Property
- <NotMapped>
- Public ReadOnly Property symbol_ever() As Byte()
- Get
- If symbol Is Nothing Then
- Return Resource2Byte("/Resources/Icons8/Color/Happy_50px.png") 'Nullbild
- Else
- Return symbol
- End If
- End Get
- End Property
- <NotMapped>
- Public ReadOnly Property solid_color() As SolidColorBrush
- Get
- If color Is Nothing Then
- Return New SolidColorBrush(Colors.Black)
- Else
- Try
- Dim v_col As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml(color)
- Dim v_color As Color = System.Windows.Media.Color.FromArgb(v_col.A, v_col.R, v_col.G, v_col.B)
- Return New SolidColorBrush(v_color)
- Catch ex As Exception
- Return New SolidColorBrush(Colors.Black)
- End Try
- End If
- End Get
- End Property
- 'Navigation Properties:
- Public Property nv_subcats As ObservableCollection(Of category_v01)
- <ForeignKey("parent_cat_id")>
- Public Property nv_subcats_back As category_v01
- Public Property nv_cat_trans2kat As ObservableCollection(Of trans_2_kat)
- Public Property transrep_2_kat2 As ObservableCollection(Of transrep_2_kat)
- Public Property nv_category_2_budcat As ObservableCollection(Of budget_2_cat_v01)
- Public Property nv_cat_2_transtemp As ObservableCollection(Of transtemp_2_kat)
- #Region "Sonderproperties"
- ''' <summary>
- ''' gibt den vollen Namen der kategorie mit ihrer Elternkategorie zurück
- ''' </summary>
- ''' <returns></returns>
- <NotMapped>
- Public ReadOnly Property fullname As String
- Get
- If parent_cat_id Is Nothing Then
- Return bezeichnung
- Else
- Return String.Concat(nv_subcats_back.bezeichnung, " - ", bezeichnung)
- End If
- End Get
- End Property
- 'Bezeichnung Properties fürs Budget:
- <NotMapped>
- Public ReadOnly Property budget_maincat_name As String
- Get
- If parent_cat_id Is Nothing Then
- Return bezeichnung
- Else
- Return nv_subcats_back.bezeichnung
- End If
- End Get
- End Property
- <NotMapped>
- Public ReadOnly Property budget_subcat_name As String
- Get
- If parent_cat_id Is Nothing Then
- Return ""
- Else
- Return bezeichnung
- End If
- End Get
- End Property
- <NotMapped>
- Public Property subcats_aktiv As ListCollectionView
- <NotMapped>
- Private WithEvents nv_subcats_helper As ObservableCollection(Of category_v01)
- Sub New()
- nv_subcats_helper = nv_subcats
- subcats_aktiv = New ListCollectionView(nv_subcats_helper)
- subcats_aktiv.LiveFilteringProperties.Add("aktiv")
- subcats_aktiv.IsLiveFiltering = True
- End Sub
- #End Region
- End Class
Im Modell schaut es so aus:
VB.NET-Quellcode
- Imports System
- Imports System.Data.Entity
- Imports System.Linq
- Public Class hhhb2_model
- Inherits DbContext
- Public Sub New()
- MyBase.New("name=hhhb2_model")
- Database.SetInitializer(New DropCreateDatabaseAlways(Of hhhb2_model))
- End Sub
- Public Overridable Property category_v01 As DbSet(Of category_v01)
- End Class
(Modell ist stark verkürzt wegen Übersichtlichkeit)
Als absolute Verzweiflungstat habe ich jetzt das gesamte Modell in ein neues Projekt übertragen und dort funktioniert es komischerweise....
Hat jemand von euch einen Tipp für mich?
Danke
mfG.
Stephan
Stephan