我相信大多數人會問爲什麼不行。我想通過詢問爲什麼這會起作用來混淆它。實體框架 - 爲什麼這會起作用?
private SmokeFireDBEntities dbContext = null;
private IList<MemberResponse> gridData = new List<MemberResponse>();
private void UserControl_Initialized(object sender, EventArgs e)
{
this.dbContext = new SmokeFireDBEntities();
var members = from m in dbContext.Members
where new[] { "A", "P", "S", "J" }.Contains(m.Class.ShortName)
orderby m.Line
select m;
foreach (Member m in members)
{
MemberResponse mr = new MemberResponse();
mr.MemberID = m.ID;
mr.Member = m;
this.gridData.Add(mr);
}
PercentageGrid.ItemsSource = this.gridData;
}
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
AlarmTotal at = new AlarmTotal();
at.Month = Convert.ToByte(this.MonthField.Text);
at.Year = Convert.ToInt16(this.YearField.Text);
at.NumAlarms = Convert.ToInt16(this.TotalAlarmsField.Text);
this.dbContext.AlarmTotals.AddObject(at);
this.dbContext.SaveChanges();
// WHY IS THE FOLLOWING CODE NOT NECESSARY???
//foreach (MemberResponse mr in this.PercentageGrid.Items)
//{
// mr.AlarmTotalID = at.ID;
// this.dbContext.MemberResponses.AddObject(mr);
//}
//this.dbContext.SaveChanges();
}
<UserControl.Resources>
<DataTemplate x:Key="NameColumnTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Member.LastName}" />
<TextBlock Text=", " />
<TextBlock Text="{Binding Path=Member.FirstName}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="InputColumnTemplate">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=NumAttended}" Name="MonthResponse" Width="60" />
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid Background="WhiteSmoke" Height="353" Width="509">
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,33,0,0" Name="MonthField" VerticalAlignment="Top" Width="75" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="93,33,0,0" Name="YearField" VerticalAlignment="Top" Width="59" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="158,33,0,0" Name="TotalAlarmsField" VerticalAlignment="Top" Width="115" />
<ListView Margin="1,67,0,0" Name="PercentageGrid" ItemsSource="Binding" HorizontalAlignment="Stretch" Width="507" Height="286" VerticalAlignment="Stretch">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Name" CellTemplate="{StaticResource NameColumnTemplate}" />
<GridViewColumn Header="Line#" DisplayMemberBinding="{Binding Path=Member.Line}" />
<GridViewColumn Header="Class" DisplayMemberBinding="{Binding Path=Member.Class.ShortName}" />
<GridViewColumn Header="Response" CellTemplate="{StaticResource InputColumnTemplate}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
我已經刪除了不必要的代碼來縮短這一點。我對C#,.NET以及與之相關的一切都是全新的。我完全難以理解爲什麼這一切都可行。當我調用第一個dbContext.SaveChanges()將記錄保存到「AlarmTotals」時,它也同時保存了所有「MemberResponse」記錄,更令人驚訝的是填充了正確的AlarmTotals.ID字段。這個真的讓我失望,我不明白這是如何運作的,看起來像是魔法。
任何洞察力和解釋將不勝感激。我真的很想了解這裏發生了什麼。
是的,我確定正確的AlarmTotalID已添加到MemberResponse記錄中。這令我驚訝的是比添加的記錄更多,因爲我沒有辦法讓它爲此獲得正確的上下文。也許它只是使用最後添加的記錄?我會跟進你關於斷點的建議,看看我能否更好地理解正在發生的事情。謝謝 – 2012-03-30 01:38:34
也許在數據庫中有一個默認或觸發器應用這些更改? – surfen 2012-03-30 01:40:49
@NickSperling ands surfen:我同意這個答案。這是不可能的,'mr.AlarmTotalID'將被設置爲你正在顯示的代碼中新創建的ID。您只需創建一個新的'AlarmTotal',您設置了三個標量屬性(我猜這些不是FK屬性),並且沒有導航屬性到另一個實體。沒有其他實體提到新的'at'。設置AlarmTotalID必須發生在別的地方。也許斷點測試會顯示。 – Slauma 2012-03-30 11:37:42