2011年10月22日

[WP7]繼承自ListBox的自訂控制項

要撰寫一個繼承自ListBoxItem的自訂控制項, 照著書上說的把base class改成ListBoxItem, 但是在compile時出現*.g.i.cs錯誤. 其實, 除了在.cs中指定之外, 還需要修改XAML宣告如下. 把Root element的名稱改成被繼承的控制項名稱即可.

<ListBoxItem x:Class="Test.UI.Controls.TestItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    Background="{StaticResource PhoneBackgroundBrush}"
    d:DesignHeight="32" d:DesignWidth="480">
    <!—其他宣告—>
</ListBoxItem>

2011年10月18日

[EF4]Member Mapping is not specified. (How to change mapping type in an entity)

I got this error when modifying a property type of an entity from Int32 to Int64 in visual studio entity designer.

image

Solution : open .edmx in XML editor, find the property you want to change and specified desired data type in XML.

  1: <Property Name="REMOVE_QTY" Type="bigint" />

[EF4]New transaction is not allowed because there are other threads running in the session

I got “New transaction is not allowed because there are other threads running in the session”while trying to delete some records via EF4.

The code looks like the following.

  1: using (var db = new MyDB()) {
  2: 	var ds = db.MyTable.Where(x => x.EndTime.Value <= DateTime.UtcNow);
  3: 	foreach (var d in ds) {
  4: 	    db.MyTable.DeleteObject(d);
  5: 	    db.SaveChanges();
  6: 	}
  7: }


One thing we need to know about Entity Framework is that when you iterate results in a loop such as “foreach”, it actually creates a open reader to iterate data for you. So back to the codes, since we are still drawing data via an active reader, then obviously we cannot “SaveChanges” when the connection is still in use. What we need to do here is to force EF to return all records, after that, we can do the “SaveChanges” call. So simply force to retrieve all data via invoking “ToList” on the entity.



  1: using (var db = new MyDB()) {
  2: 	var ds = db.MyTable.Where(x => x.EndTime.Value <= DateTime.UtcNow).ToList();
  3: 	foreach (var d in ds) {
  4: 	    db.MyTable.DeleteObject(d);
  5: 	    db.SaveChanges();
  6: 	}
  7: }

Blog Archive

About Me