EF记录数据变更方法

在使用SQLServer时,通常情况下使用SQLServer自带管理器可以查看到数据变更的记录。如果在程序用需要更灵活的记录这些信息,则可以通过EF的ObjectStateManager属性的相关方法实现。

ObjectStateManager.GetObjectStateEntries 方法

返回具有给定状态的对象或关系的 ObjectStateEntry 对象的集合。

命名空间: System.Data.Objects

程序集: System.Data.Entity(在 System.Data.Entity.dll 中)

阅读更多

删除ListView(ListBox)中Binding的item

使用EF框架并在在ListView或ListBox中binding了数据源,每一行数据都对应一个删除按钮,点击这个按钮就删除对应的那条数据。

看似简单的功能也能玩出新花样,下面提供两种方案:

【方案一】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<ListBox FontSize="20" SelectionMode="Single" ItemsSource="{Binding XX}" >
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<Button Width="25" DockPanel.Dock="Right" Height="25" Style="{x:Null}" Tag="{Binding}" Background="{x:Null}" BorderBrush="{x:Null}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="Del" TargetObject="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
<TextBlock x:Name="txtContent" DockPanel.Dock="Left" TextAlignment="Center" Text="{Binding xx,Mode=TwoWay}" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
1
2
3
4
5
6
7
8
9
10
11
12
13
public void Del(object sender, RoutedEventArgs e)
{
if (sender != null && sender is Button)
{
if (((Button)sender).Tag != null && ((Button)sender).Tag is XX)
{
if (Utils.ShowMessage(this, "确定要删除吗?", "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
DelOper();
}
}
}
}
阅读更多

VS2012/2013无身份验证远程调试

在VS2012之前的版本中,如果要使用remote debug需要注意一下几种情况:

  • 不能使用“无身份验证”模式
  • 如果不是域环境,本地计算机和远程计算机必须使用相同的系统登陆用户名和密码
  • 远程计算机调试使用的程序需要包含对应的“.pdb”文件

这些条件使得VS远程调试变得非常麻烦,但是从VS2012开始,远程调试功能有了改善,下面介绍一下VS2012或VS2013使用“无身份验证”模式进行远程调试的方法。(以VS2013为例)

1.进入VS安装目录,按如下路径找到Remote Debugger文件夹

\Microsoft Visual Studio 12.0\Common7\IDE\Remote Debugger

阅读更多

浅析StaticResource和DynamicResource

初学WPF的新人有时都会对StaticResource和DynamicResource的概念及应用朦朦胧胧,这里通过一个小例子彻底弄懂这两者的区别及用法。

首先是前台XAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<Window x:Class="WpfApplication50.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="Brush" Color="Red" />
</Window.Resources>
<StackPanel>
<Button Margin="5" Content="Change Resource" Click="Button_Click" />
<Label x:Name="lbl1" Margin="5" Content="Static Resource(Window.Resources)" Background="{StaticResource Brush}" />
<Label x:Name="lbl2" Margin="5" Content="Static Resource(Label.Resources)" Background="{StaticResource Brush}" >
<Label.Resources>
<SolidColorBrush x:Key="Brush" Color="Blue" />
</Label.Resources>
</Label>
<Label x:Name="lbl3" Margin="5" Content="DynamicResource(Window.Resources)" Background="{DynamicResource Brush}" />
<Label x:Name="lbl4" Margin="5" Content="DynamicResource(Label.Resources)" Background="{DynamicResource Brush}" >
<Label.Resources>
<SolidColorBrush x:Key="Brush" Color="Blue" />
</Label.Resources>
</Label>

</StackPanel>

</Window>

后台的代码:

1
2
3
4
5
private void Button_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
this.Resources["Brush"] = brush;
}
阅读更多

WPF隐藏ListView中GridView的列

有需求要根据某变量的值将ListView中GridView的某一个GridViewColumn做隐藏设置,经过一番思考,总结了两种方法,但都需要对ListView中的GridView以及每一个GridViewColumn做命名:

1
2
3
4
5
6
7
8
<ListView x:Name="lvTest"  SelectionMode="Single">
<ListView.View>
<GridView x:Name="gvTest" >
<GridViewColumn x:Name="gvcName" Width="85" />
<GridViewColumn x:Name="gvcAge" Width="85" />
</GridView>
</ListView.View>
</ListView>

方法一:设置列宽

这种方法相对简单,如果没有特殊条件限制,可以直接设置某一列的width=0,如:

gvcName.Width = 0

阅读更多

Jekyll站内搜索:jekyll-lunr-js-search使用说明

jekyll-lunr-js-search是一款适用于Jekyll的站内搜索插件,相关介绍和使用方法在github中已经有简单说明,不过我却在使用这个插件上耽误了很多时间,现在整理一番:

【安装】

1.clone “jekyll-lunr-js-search”到本地

2.将clone后的jekyll-lunr-js-search项目中的

build/jekyll_lunr_js_search.rb

阅读更多

WPF获取ContextMenu的源

在WPF中若使用多个控件,并给这些控件添加同一个ContextMenu,那么如何获取ContextMenu的鼠标点击源?

WPF的ContextMenu并没有Winform的ContextMenu的SourceControl这一属性,但我们也可以通过另一种方法实现同样的功能。

在ContextMenu的MenuItem的点击事件中使用ContextMenu.PlacementTarget

1
2
3
4
5
public void MenuItem_Click(object sender, RoutedEventArgs e)
{
ContextMenuService.GetPlacementTarget(
LogicalTreeHelper.GetParent(sender as MenuItem));
}

EF一对多关系删除问题

在使用EF删除一对多关系的实体时,碰到如下提示:

“其他信息: 操作失败: 无法更改关系,因为一个或多个外键属性不可以为 null。对关系作出更改后,会将相关的外键属性设置为 null 值。如果外键不支持 null 值,则必须定义新的关系,必须向外键属性分配另一个非 null 值,或必须删除无关的对象。”

原因很简单,假设一个A对应多个B,那么B中的外键就是A的主键,如果先删除A,接着又把B读取到datacontext中

var lstB = a.B.ToList();

container.A.DeleteObject(a);

就会导致B中的外键失去关联,所以便产生了错误,解决的办法有下面几种:

阅读更多

Windows平台下使用Jekyll遇到的问题

本机操作环境为win8.1 64bit,在使用Jekyll搭建主页过程中遇到了一些问题,整理如下:

1.安装nokogiri

使用jekyll lunr js search等插件时需要用到nokogiri,用

gem install nokogiri json

安装时出现错误libxml2.dll is missing. 经过排查,发现使用的ruby2.0 64bit对nokogiri 32bit不兼容,卸载ruby2.0 安装1.9.3 32bit版本即可正常使用。

阅读更多

125 Basic C# Interview Questions and Answers

  最近把wordpress迁到github,但发现之前的域名已经过期了,但是wordpress的后台还是绑定了这个域名,导致没办法登陆后台。后来发现修改数据库即可解决,方法如下:

  1. 用phpmyadmin登录数据库
  2. 找到“wp_options”表
  3. 修改“siteurl”字段为新的域名