Facebook lost some data

I’m not sure how widespread this was, but I got an email from Facebook saying:

Subject: Please reset your email notification settings.

Unfortunately, the settings that control which email notifications get sent to you were lost. We’re sorry for the inconvenience.

To reset your email notification settings, go to:

http://www.facebook.com/editaccount.php?notifications

Thanks,

The Facebook Team

And on the Facebook home page, this appears at the top:

image

Apparently, it’s quite widespread, but that’s only amongst people who have written about it in blogs, etc.

No matter how widespread it is, it’s a pretty huge stuff up. They lost a lot of data. Presumably they underwent a rather large data migration. And it must be easier for them to ask users to redo their settings rather than to restore them from a backup.

But is the bad PR worth it? If they are this careless with email settings (which could actually have some effect under anti-spam legislation – if you’ve already opted out of some types of communication and they start sending it again, even despite this notification, they could be in trouble), who’s to say they aren’t going to be this careless about things like advertising payments?

Windows Azure Development Storage with real SQL Server

I don’t have SQL Server Express, I’ve got the real deal. Development Storage for Windows Azure assumes you are using SQL Server Express with the instance name SQLEXPRESS.

Thanks to this article I now know that you just need to edit the config file for DevelopmentStorage.exe, which is usually located in C:Program FilesWindows Azure SDKv1.0binDevelopmentStorage.exe.config.

There are two spots to modify. The first is the connection string (XPath: /configuration/connectionStrings/add/@connectionString) and the second is the dbServer attribute of the Table service (XPath: /configuration/developmentStorgeConfig/service/service[@name='Table']/@dbServer).

And then it will work. Enjoy!

SSH slow to login? Disable reverse DNS lookup

By default in Ubuntu (and probably other distributions), when you log into the SSH server, it will do a reverse DNS lookup of the client for security reasons.

That’s fine, but it’s kind of annoying when you don’t have a working reverse DNS for your IP address. Like for all my internal addresses.

To turn it off (thanks to http://ubuntuforums.org/showthread.php?t=577616):

Edit /etc/ssh/sshd_config and add the following line:

UseDNS no

Easy!

Declarative data binding on User Controls

I had this problem a few years ago and figured that there was no good solution, but I am older and wiser now so figured it’s time look for a better solution.

I have a GridView, and inside the ItemTemplate I call one of my own UserControls:


<asp:GridView ID="grdMessages" runat="server" ... >
  ...
  <ItemTemplate>
    <uc1:EmailAddress ID="EmailAddress1" runat="server" 

      DataSource='<%# (MailAddress)Eval("FromAddress") %>' />
  </ItemTemplate>
  ...
</asp:GridView>

But the DataSource property of the EmailAddress user control is never set. If I have a plain <%# Eval("FromAddress") %> right next to the <uc1:EmailAddress /> tag, the literal evaluates properly. The user control doesn’t.

My original solution was to just set the value of the user control’s properties in the code behind class. But I shouldn’t have to do that. (It’s also a bit annoying to do that because I’d have to handle the GridView’s DataBinding event and then find the control and update it.)

There’s got to be a better way!

I overrode the user control’s DataBind and DataBindChildren methods, to see if they were being called. They were.

So then I tried it with a Literal control, setting the Text property. Probably the simplest .NET Web Control there is. Obviously that worked, because that’s how data binding is supposed to work. So there must be something that the Literal control does that mine doesn’t. Time to pull out Reflector!

The Literal.Text property has an attribute called Bindable. Could this be it? It makes sense. Its full name is System.ComponentModel.Bindable. Does it work? Nope.

Hang on a tick… the DataSource property is being set now. So what is my problem? The code that’s doing the work is in the Load event handler. But the DataSource property is being set after Load. So I’ve moved that code to PreRender and…

It works!

So what have we learnt?

To make a UserControl’s property bindable, you must use the [Bindable] attribute. And those properties will not get bound until after Load, so don’t put code that relies on your bound properties in the Load event handler!

ObjectDataSource.SelectCountMethod wants an int

I have been working with the GridView ASP.NET control today, bound to an ObjectDataSource. This is to do with my experiment with DBMail that I mentioned the other day. It wasn’t working. I couldn’t find a solution on the net. By chance, I changed this:

    class DatabaseSource
    {
        long _messageCount = 0;

        public List<Message> GetMessages(long startRow, int pageSize)
        {
            Database db = new Database();
            List<Message> messages = db.GetMessages( startRow, pageSize,


               out _messageCount);

            return messages;
        }

        public long GetMessageCount()
        {
            return _messageCount;
        }
    }

into this:

    class DatabaseSource
    {
        long _messageCount = 0;

        public List<Message> GetMessages(long startRow, int pageSize)
        {
            Database db = new Database();
            List<Message> messages = db.GetMessages( startRow, pageSize,


               out _messageCount);

            return messages;
        }

        public int GetMessageCount()
        {
            return Convert.ToInt32(_messageCount);
        }
    }

Notice the difference?

To get the ObjectDataSource to work, the SelectCountMethod has to return an 32-bit integer (System.Int32). It failed (without an error message) when it was provided with a long or 64-bit integer (System.Int64).

It turns out it’s written in the documentation in black and white:

Type: System.String

A string that represents the name of the method or function that the ObjectDataSource uses to retrieve a row count. The method must return an integer (Int32). The default is an empty string ("").

Who would have thought?

Hopefully this will save someone who is running into the same problem. (That’s assuming that this will pop up in their search results when they search for ObjectDataSource and GridView paging not working!)

Stop X from starting when booting Ubuntu

This is nothing new, but I wanted to stop X from starting on bootup with Ubuntu Desktop 8.04. I would prefer to start it using startx.

The easy solution, from within the Ubuntu desktop (seeing X has started already, I may as well use graphical tools) click the System menu, then Services. Unlock (if you need to) and deselect Graphical login manager (gdm).

BE CAREFUL THOUGH: If you apply those changes, X will stop, presumably because it was started through the init scripts and by changing the init scripts it decided it didn’t need to be running any more. That can be a pain if, for example, you are writing a blog post on how to do it in Firefox at the time. (Thankfully the autosave feature of WordPress works!)

Entity Framework: Part 2

I have been reading a bit of the documentation about the ADO.NET Entity Framework. There are a few passages that relate to my previous post:

Because referenced objects are not automatically loaded, you must call the Load method on the entity reference to load the related object data into the object context. You can also specify a query path that defines which related objects to load with returned objects. For more information, see Querying Data as Objects (Entity Framework).

A query path definition ensures that the Course objects related to Department objects are also returned.

The code accompanying this is:

// Define a query that returns all Department objects and related
// Course objects, ordered by name.
ObjectQuery<Department> departmentQuery =
    schoolContext.Department.Include("Course").OrderBy("it.Name");

Another difference between LINQ to SQL and using the Entity Framework is that the SubmitChanges() method’s equivalent is the SaveChanges() method.

So I managed to solve my original problem of not being able to follow references in related tables/objects by using the Include method when performing my queries:

My first test method from the last post, TestFooHasTwoBars() will pass when it’s written like this (new code is highlighted):

[TestMethod]
public void TestFooHasTwoBars()
{
    testlinqEntities entities = new testlinqEntities();
    Foo foo = (from f in entities.Foo.Include("Bar") where f.FooId == 1 

        select f).First();
    Assert.IsNotNull(foo);
    Assert.AreEqual(2, foo.Bar.Count);
}

I’m not sure if there’s a way to do that in "pure" LINQ (ie without having to include the string to reference the Bar property.

Mail archiving with DBMail

I have been looking for a SQL-based way to store all my old email messages. I think I may have found it. There is an email system called DBMail that stores messages in a database (MySQL, PostgreSQL or SQLite) and exposes them (for want of a better word) through POP3 and IMAP.

At the moment I use Gmail, forwarding to Exchange 2007, Outlook 2007 and a huge PST. Searching the PST takes a while. Gmail doesn’t have everything in it. Exchange is there to push new messages to my phone (though I haven’t been using that since I’m back in NZ because data charges on prepay are too high, even though they have come down significantly in recent months). But I am getting a new phone soon, which should support IMAP IDLE push mail, which means I can get rid of the Exchange hosting.

The good thing about DBMail is that seeing it will store everything in MySQL (I’m going to use MySQL), I’ll be able to do anything I want with the database. So I’ll be able to write a web-based interface to it.

One thing I am going to have to think about is search. MySQL has full text indexing, but I’m not sure how good it will be with over 2.5GB to index. The other option I know of and have used a bit is Apache Lucene. I read an article by Jayant Kumar comparing the two. It looks like seeing I have so much data, Lucene might be the better option. I’ll try out native full text first and see if it’s fast enough, and then try out Lucene. And maybe I will post the results.

So I am now installing Ubuntu Desktop 8.04 on Virtual PC (with help from this TechRepublic article) and will give it a whirl. I have a server running Ubuntu Server in the UK, which is where this will live long term, if it works.

LINQ to Entities – my first steps

I have had little bit of experience with LINQ to SQL, but recently read that Microsoft is instead concentrating on LINQ to Entities. I have just started a new project, so thought decided to use LINQ to Entities instead. And I ran into a problem. I haven’t managed to find a solution to it yet, so I figured that I would write about it here and hopefully, once I have figured out what I was doing wrong, it might be able to help someone else out.

The problem is that I have a site using ASP.NET MVC (though that’s not really relevant) that uses the normal ASP.NET SQL membership provider. Then I have a table that has a foreign key reference to the aspnet_Users table:

So far, so good. I have created a user, and manually created a Foo and given it two Bars.

Now I create the ADO.NET Entity Data Model, generated from the database. I only want three tables in my model: aspnet_Users, Foo and Bar.

Not a lot is different there. In the end I want aspnet_Users to just be called User, but let’s just see if this is working out of the box. My prediction (based on how it actually worked in my real project, is that the reference between Bars and Foos is fine, but between Users and Foos will not work. Let’s see…

[TestMethod]
public void TestFooExists()
{
    testlinqEntities entities = new testlinqEntities();
    Foo foo = (from f in entities.Foo select f).First();
    Assert.IsNotNull(foo);
}

That test passed.

Next is to check that the Foo who’s ID is 1 has got two Bars.

[TestMethod]
public void TestFooHasTwoBars()
{
    testlinqEntities entities = new testlinqEntities();
    Foo foo = (from f in entities.Foo where f.FooId == 1 select f).First();
    Assert.IsNotNull(foo);
    Assert.AreEqual(2, foo.Bar.Count);
}


This is a bit of a surprise. It failed. I can’t think why. Let’s check to see whether the first Bar has got a Foo:

[TestMethod]
public void TestBarHasAFoo()
{
    testlinqEntities entities = new testlinqEntities();
    Bar bar = (from b in entities.Bar select b).First();
    Assert.IsNotNull(bar, "Could not find a Bar.");
    Assert.IsNotNull(bar.Foo, "Bar has no Foo.");
}

No, that failed too. So none of the references are working. They worked out of the box with LINQ to SQL.

For good measure, I’ll write two more tests:


[TestMethod]
public void TestFooHasUser()
{
    testlinqEntities entities = new testlinqEntities();
    Foo foo = (from f in entities.Foo where f.FooId == 1 select f).First();
    Assert.IsNotNull(foo);
    Assert.IsNotNull(foo.aspnet_Users);
}

[TestMethod]
public void TestUserHasAFoo()
{
    testlinqEntities entities = new testlinqEntities();
    Guid userId = new Guid("8c3efb04-80aa-4b6b-af11-98445a08f4ea");
    aspnet_Users user = (from u in entities.aspnet_Users where 

        u.UserId == userId select u).First();
    Assert.IsNotNull(user);
    Assert.IsTrue(user.Foo.Count > 0);
}

These failed too. As expected.

So, I am quite surprised that none of the relationships are actually working. Was it a fluke that they worked earlier?

No, I wrote another test, that gets a Bar’s Foo’s User’s ID:


[TestMethod]
public void TestGetUserIdOfFooOfBar()
{
    Guid expectedUserId = new Guid("8c3efb04-80aa-4b6b-af11-98445a08f4ea");
    testlinqEntities entities = new testlinqEntities();
    Guid userId = (from b in entities.Bar where b.BarId == 1 

        select b.Foo.aspnet_Users.UserId).First();
    Assert.AreEqual<Guid>(expectedUserId, userId);
}

And that one passes.

So, while in LINQ to SQL you could keep on following references after you had performed your original query, you can’t do that with LINQ to Entities. Well, not automatically, at least. That’s a bit of a disappointment. That means more work for me.

I just had a look at the Foo.Bar property. There is an IsLoaded property and a Load() method. If you Load() the Bar first, then the count works. LINQ to SQL didn’t need such as method, I guess, because it was always working against the database. I should probably read up more on this. It doesn’t seem right to call Load() which will probably load all of the properties of the "child" objects, when I only need one of those properties, or only need the first one.

I will investigate further and hopefully remember to post a followup.

But in summary, the IRelatedEnd.Load() method (the EntityCollection class derives from RelatedEnd which implements IRelatedEnd – sorry I can’t be bothered with the namespaces). It’s that method that will load the collection’s contents. That’s for one-to-many relationships. For the many-to-one and one-to-one side of things, I used, for example, Bar.FooReference.Load(). (FooReference is of type EntityReference<Foo>, which ultimately derives from RelatedEnd as well).