我有一个具有这种结构的模型
1 - 表
2 - 型号
3-表B
定义: Table MODEL 与 TABLEA 具有一对多关系 Table MODEL 与 TABLEB 具有一对多关系 TABLEA 与 TABLEB 具有多对多关系
这些表中的每一个都具有这种类型的模型定义:
With modelBuilder.Entity(Of TABLEA)()
.Property(Function(t) t.dbTableAID).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasKey(Function(t) t.dbTableAID)
.Property(Function(t) t.Name).
HasColumnName("TableName")
.ToTable("TI")
每种类型定义如下:
Public Class Model
Public Sub New()
Me.TableAs = New HashSet(Of TableA)()
Me.TableBs = New HashSet(Of TableB)()
End Sub
Public Property model_id as integer
Public Overridable Property TableAs() As ICollection(Of TableA)
Public Overridable Property TableBs() As ICollection(Of TableB)
Public Sub LoadModel(Query)
For Each objTableA In Query.cbsSrcCubes
Dim objDb As TableA = New TableA()
Me.TableAs.Add(objDbSrcCube.CastCubeToDB(objDb))
Next
For Each objTableB In Query.cbsSrcCubes
Dim objDb As TableB = New TableB()
Me.TableBs.Add(objDbSrcCube.CastCubeToDB(objDb))
Next
End Sub
End Class
Public Class TableA
Public Sub New()
Me.TableBs = New HashSet(Of TableB)()
End Sub
Public Property TableA_id as integer
Public Overridable Property Model() As Model
Public Overridable Property TableBs() As ICollection(Of TableB)
End Class
Public Class TableB
Public Sub New()
Me.TableAs = New HashSet(Of TableA)()
End Sub
Public Property TableB_id as integer
Public Overridable Property Model() As Model
Public Overridable Property TableAs() As ICollection(Of TableA)
Public Sub LoadTableA(Query)
For Each objTableA In Query.TableACol
Dim objDb As TableA = New TableA()
Me.TableAs.Add(objDb)
Next
End Sub
End Class
这是上下文的定义
Public Class QDModelContext
Inherits DbContext
Public Property Models() As DbSet(Of Model)
Public Property TableAs() As DbSet(Of TableA)
Public Property TableBs() As DbSet(Of TableB)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
modelBuilder.Conventions.Remove(Of PluralizingTableNameConvention)()
'Specific Features of the MODEL Objects
With modelBuilder.Entity(Of Model)()
.Property(Function(t) t.model_id).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasKey(Function(t) t.model_id)
.Property(Function(t) t.Name).
HasColumnName("ModelName")
End With
With modelBuilder.Entity(Of TableA)()
.Property(Function(t) t.tablea_id).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasKey(Function(t) t.tablea_id)
.Property(Function(t) t.Name).
HasColumnName("TableAName")
modelBuilder.Entity(Of TableA)().
HasMany(Function(t) t.TableBs).
WithMany(Function(t) t.TableAs).
Map(Sub(m)
m.ToTable("TableA-TableB")
m.MapLeftKey("tablea_id")
m.MapRightKey("tableb-id")
End Sub)
End With
With modelBuilder.Entity(Of TableB)()
.Property(Function(t) t.tableb_id).
HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasKey(Function(t) t.tableb_id)
.Property(Function(t) t.Name).
HasColumnName("TableBName")
End With
End Class
从 Main 调用的保存例程:
Public Function PersistModel(CurrentModel As Model)
Try
Using context As New QDModelContext()
Dim ModelObj As Model = New Model()
context.Models.Add(ModelObj)
context.SaveChanges()
End Using
Catch ex As Exception
buildError("Couldn't Persist Model.", ex.Source & ": " & Err.Description, ex)
End Try
End Function
我尝试正确定义 FK 关系并遇到以下错误:
modelBuilder.Entity(Of TableB)().
HasRequired(Function(t) t.Model).
WithMany(Function(t) t.TableBs).
Map(Sub(m)
m.MapKey("ModelID_FK")
End Sub)
它工作正常......但只要我添加多对多关系,它就会说它无法将某些对象从 TableA 映射到模型。
所以如果我将它设置为可选:
modelBuilder.Entity(Of TableB)().
HasOPTIONAL(Function(t) t.Model).
WithMany(Function(t) t.TableBs).
Map(Sub(m)
m.MapKey("ModelID_FK")
End Sub)
它工作正常,它创建了连接表 TableA-TableB,但它在表 B 中添加了一些行重复,其中 ModelID_FK 空值......我认为表 B 中不存在关系 TableA-TableB B 值的那些。奇怪.. .
我想知道它是否与加载顺序或实体之间的某些循环或顺序有关...
任何帮助将不胜感激。谢谢!!!