我在VB.NET中的EF6项目使用以下映射映射到MySQL数据库:
Partial Public Class WorkRule
Public Property WorkRuleID As Integer 'Maps to int
Public Property Description As String 'Maps to tinytext
Public Property RegularTime As Byte() 'Maps to tinyblob
End Class
如果我编辑“说明”文本框,则会在调用.SaveChanges时检测到并按预期保存更改。但是,如果我编辑绑定到数组元素的其中一个文本框的内容,则更改绑定元素,但DBContext.ChangeTracker不会检测到更改,并且在调用.SaveChanges时更改不会保存到数据库中。
我可以通过手动设置DBContext.Entry()强制保存.State = EntityState.Modified但是有更好的方法来执行此操作,因此会自动检测对数组元素的更改吗?我应该使用除数组之外的东西吗?
代码如下:
XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListBox x:Name="lstWorkRules"/>
<StackPanel Grid.Column="1" DataContext="{Binding ElementName=lstWorkRules, Path=SelectedItem}">
<Label>Description:</Label>
<TextBox Text="{Binding Path=Description}"/>
<Label>Day 1:</Label>
<TextBox Text="{Binding Path=RegularTime[0]}"/>
<Label>Day 2:</Label>
<TextBox Text="{Binding Path=RegularTime[1]}"/>
<Label>Etc.</Label>
<Button Click="Button_Click">Save</Button>
</StackPanel>
</Grid>
VB:
Imports System.Data.Entity
Class MainWindow
Dim wrContext As WorkRuleEntities
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
wrContext = New WorkRuleEntities
wrContext.WorkRules.Load
lstWorkRules.ItemsSource = wrContext.WorkRules.Local
lstWorkRules.DisplayMemberPath = "Description"
End Sub
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
wrContext.SaveChanges()
End Sub
End Class