Subscribe via Email Subscribe

Tuesday, September 22, 2009

Entity Framework messes up Primary Keys of Views - A work around

After adding a couple of Views to my Entity Data Model, I noticed that EF had messed up the Primary keys of my entities (based on the views). It was picking a lot of fields as Primary, even when they are not Primary. Obvious solution was to manually do what EF should have done for me. So I went ahead and fixed the keys in the Model.

Lo and Behold! I was greeted with an error that all key properties of the entity set must be assigned to all key properties of the table.... WTF?

Oh! Ok... I know EF maps the Store with the Model. I have fixed the Model... but not the Store yet.

To my surprise, you can not edit the store from the designer. Never mind, its just XML, I can edit it using any text editor. So I went ahead and edited the .EDMX XML. I deleted all the incorrect Key entries. For example, I changed this:

<Key>
      <PropertyRef Name="HouseID" />
      <PropertyRef Name="State" />
      <PropertyRef Name="OtherCrapKey" />
</Key>

to this:

<Key>
      <PropertyRef Name="HouseID" />
</Key>

This fixed the problem... but only for a while. I knew that every time I change the schema (add / delete / modify a view for my Model), I'd have to "Update Model from Database", that'll again screw up the entire Store.

Googling the issue led to the following post:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ea0bf748-bc97-439d-99b0-76180b2161bb?prof=required

So I knew the issue was Non-Nullable fields in the views. And the work around was simple. I made all the non-primary fields as Nullable. For example, if you have the following select statement in the view:

Select idKey, fkidHouseType, 'Vic' State
From tblHouse

This creates all the three fields as Primary in the EF Store (Even when only idKey should be Primary).

Changing it to the following fixes the issue:

Select idKey, Cast(fkidHouseType As Int) fkidHouseType, CAST('Vic' AS VARCHAR(3)) State
From tblHouse

I know it's a work around, but for now, I don't have to manually change the EDMX XML every time I update the schema.

Let me know if you find a better solution or a more efficient way of specifying Nullable values in a View.


Read more!

Friday, August 14, 2009

Bulleted list in Silverlight

While working on a Silverlight 3 application, I needed a bulleted list similar to an unordered list in html. I simply needed to display an Array as a bulleted list.To my surprise, there is no support for this in silverlight. Even in version 3. C'mon, I thought Microsoft gets it right in version 3?

Google (and this time I even tried Bing) only returned the following relevant links:

http://www.codeproject.com/KB/silverlight/OrderedList_Silverlight.aspx
http://silverlight.net/forums/t/5148.aspx

Numbered And Bulleted Lists for Silverlight By Matt Perdeck on Code Project looked very promising, but its more than an year old, some users have commented that it doesn't work, and the author seems to have disappeared as he never replied to any comments. Nah... don't wanna use such a project for a production application.

After experimenting a while with ListBox I came up with the following solution:

<ListBox ItemsSource="{Binding MyArray}" Background="Transparent" BorderThickness="0">
<
ListBox.ItemTemplate>
<
DataTemplate>
<
fxui:HStackPanel>
<
TextBlock Text="• "></TextBlock>
<
TextBlock Text="{Binding}"></TextBlock>
</
fxui:HStackPanel>
</
DataTemplate>
</
ListBox.ItemTemplate>
</
ListBox>

You can replace fxui:HStackPanel with a standard StackPanel with a Horizontal layout. It works like a charm.

PS: If you are interested in MVVM for silverlight, have a look at the SilverlightFX framework by Nikhil.

Read more!

Thursday, June 18, 2009

Silverlight Slider - Jump Value by SmallChange when dragged

Not long ago, I needed a similar functionality.

But my requirement wasn't just whole numbers. I wanted the slider to jump by what ever I set SmallChange to (for e.g. 1000). So the range could be... 3000 to 7000 with possible values = (3k, 4k, 5k, 6k and 7k) and the Slider should snap to the corresponding values while dragging.

The formula to get the values was very simple and straight forward: (Math.Round((Val - Min)/Sml) * Sml) + Min

But How do I coerce the value to that?

Hey! Value is a DependencyProperty... right?
So I can use coercion... May be Slider has a Coerce method that coerces the value based on Min, Max etc. and I can derive from it and override the method so it'll respect the SmallChange property as well?

I fired up Reflector to inspect System.Windows.Controls.Slider.
Hmm... Doesn't have any coercion, but the OnValueChanged method is simply calling the base.OnValueChanged... and Slider derives from RangeBase

Aha! RangeBase has a CoerceValue method.
D'Oh... its private... not protected. Crap!
I could have simply overridden this one method in my derived MyRangeBase and be done with it.

Wait a minute... didn't Microsoft release the source code for most of the Silverlight Controls?

Thats it... I copied the RangeBase.cs, Slider.cs and VisualStates.cs from the above source code and modified the following:

1. Renamed RangeBase to MyRangeBase (and let VS do the magic)
2. Renamed Slider to MySlider (and again let VS do the work for me)
3. Change base class of MySlider to MyRangeBase
4. Changed the CoerceValue() of MyRangeBase to

private void CoerceValue()
{
double minimum = Minimum;
double maximum = Maximum;
double value = Value;
double coercedValue = _requestedVal;

if (SmallChange != 0)
{
coercedValue = (Math.Round((_requestedVal - minimum) / SmallChange) * SmallChange) + minimum;
}

if (coercedValue != value && coercedValue >= minimum && coercedValue <= maximum)
{
SetValue(ValueProperty, coercedValue);
}
else
{
if (value < minimum)
{
SetValue(ValueProperty, minimum);
}
if (value > maximum)
{
SetValue(ValueProperty, maximum);
}
}
}


That's it. It worked like a charm :)



Enjoy!



Read more!

Friday, October 24, 2008

Astoria futures: offline enabled data services - Scenario and Requirements Discussion

Are you a software architect facing a challenge to design applications that work like Outlook?
(I know that's a vague description, but that's what you get from clients... don't you?)
Do you have a requirement where applications work offline most of the time and then sync back to the central database?

If you have implemented one-off custom solutions for offline syncing, then Astoria Offline is the way to go!

If you didn't attend Mix 08 or haven't heard of Astoria Offline, there is a msdn blog by Pablo Castro on Astoria futures: offline enabled data services.

My post is simply to discuss:

  • various scenarios where Astoria Offline could be useful,
  • what is the existing architecture of these applications,
  • how you envision your apps in the future (technologies and architecture)
  • and what features you'd like to see in Astoria Offline

I have created a poll on this blog to get a feedback on (if and) how you plan to use Astoria Offline when it is released.

Our Scenario, Existing Architecture, and Project Requirements:

I work for a construction company that has sales consultants (SC) and construction supervisors (CS) on field.
We have a centralised end to end work flow system (from leads to sales to construction to settlement).

At different stages of this work flow, we have different set of requirements.
For example,

  • The SC and CS modules would be deployed on PDAs (ideal candidates for Astoria Offline)
  • Some parts of the system our going to be web based (like System Administration etc)
  • Some parts (that use Autocad / image processing or other processor intensive tasks) are deployed on the desktop.

At the moment, our entire system is web based (.Net 2.0 with some jQuery goodness for desktop browsers)
So the PDAs have to be online all the time. We did implement our own local storage and offline syncing, but its a pain in the a$$.

My plan is to

  • Use WPF for our rich and processor intensive Desktop applications.
  • Silverlight for our internet applications (so we could reuse most of our WPF code)
  • And, html with jQuery (or Silverlight) for PDAs.

Now, all of the three would utilise a common Business Layer hooked to a Backend Data Layer. And all three would benefit if the Data layer is based on Astoria Offline. But the greatest impact will be on the PDA Apps because consultants and supervisors would be able to work offline most of the times, and sync their PDAs when online.

Your Scenario, Existing Architecture, and Plans:

Feel free to discuss how you think your organisation / applications could utilise Astoria Offline, and what features you look forward to in it?

  • What sort of data store are you most likely to use?
  • Are the applications primarily going to be on mobile devices? Desktops?
  • What front-end technologies do you plan to use? (WPF / Silverlight / html + jQuery / others)?
  • Other considerations / platform requirements / compatibility issues / concerns?

Looking forward to a healthy discussion and <wishful thinking>Astoria Offline RTW</wishful thinking>


Read more!

Monday, September 29, 2008

jQuery to ship with Microsoft ASP.Net

Awesome!

I was having a hard time trying to convince my employer to use jQuery with ASP.Net Ajax Control Toolkit.

Main issues were:
1. jQuery is not supported by Microsoft.
2. jQuery duplicates a lot of functionality that MS Ajax provides, which leads to confusion, larger footprint, and dependency on two libraries for similar functionality.

I'm glad now I can freely use the power of jQuery. Not only is Microsoft going to be shipping jQuery with Visual Studio, its also going to build controls that utilise jQuery.

But, I wonder how Microsoft will address the overlap between ASP.NET AJAX Library and jQuery. I’ve been using jQuery to complement ASP.NET AJAX, but I’ve always been concerned about the overlap between the two. Two libraries providing their own distinct methods to retrieve data, handle events etc.

Microsoft has clarified that they are going to use jQuery library "as-is". And obviously Microsoft has to keep their product backward compatible. So it seems this overlap will always exist. Thankfully, jQuery has a very small footprint, so this is not going to be a major issue for most people.

Further links on this awesome news:


Read more!

Saturday, September 27, 2008

Silverlight 2 RC0 Released for Developers

Download SL2RC0
Note... RC0 is a developer release only!
Read more on ScottGu's Blog and Tim's Blog.

Enjoy!
Read more!

Microsoft Dev Sta Challenge 2008

200 Hours, 8 Minutes. 1 Winner!
Code your way to MIX 09 in Las Vegas!

The challenge begins when the brief is released at 0808 EST on Monday 29 September. You’ll have just 200 hours and 8 minutes to prove your skills using Microsoft® Visual Studio® 2008. All entries must be submitted as a Microsoft Visual Studio 2008 project and be submitted to the devsta site before the challenge ends at 1716 EST on Tuesday 7 October 2008

http://devsta.microsoft.com.au/default.aspx

Your submission will be judged by a panel of experts, and the winner will receive an awesome DevSta prize pack worth over $18,000* RRP – including a trip to Las Vegas to attend the MIX09 Developer Conference, an an Xbox 360™ Elite console package and much more! With a total prize pool valued at over $33,000 RRP there are also fantastic runner-up prizes to be won!

If that’s not enough, there's also a special prize for the best Windows Mobile application! You could WIN two Samsung i900 Omnia mobiles worth $849 RRP each and a Samsung 40" series 6 LCD TV valued at $3099 RRP!

Read more!
Ok, The problem is... If I don't put this text, then the main-wrap div shrinks to fit the text in the post (except in IE). In IE (always the odd one out), the layout is how I want it to be. Placing this long line of text fixes the issue in other browsers as well. I know it's a hack...I'll fix the layout when I get some time. Cheers :)