0

Entity Framework – usage hurdles and tips

Well… let me start with a counter to this blog title! I actually like Entity Framework and don’t mind spending time on getting things done with it.
But, I have to be honest, some things just don’t seem to be right when I’m using it.
It could all just be me, but let me get my message out and share my experience. So here goes… my current hurdles with Entity Framework.

First of, some common issues are already listed on other sites. So first go check this list of issues and then read this post on how to tackle some of those items!

Secondly, most of my ‘problems’ are related to the interaction of the Winforms DataGridView control with EF, so maybe not everything is really due to EF itself.

  • Missing metadata
  • This element is not actually true, because EF has all the metadata a programmer needs. But it’s to far hidden, in my opinion!
    Lets give an example… in my program I want to have several managing forms that actually do nothing more than CRUD actions. So the setup of each form is almost the same, the only thing that is different is the Entity Objects being used in the Grid to show the data.
    But due to the nature of the DataGridView and the usage of ObjectQuery as BindingSource, you will get all the columns that are defined in the ObjectQuery. Meaning Key columns and relational mapping columns will appear inside the grid!
    This is something I don’t want to show inside my grid ( this is my setup, so maybe for some people those columns can be relevant ).

    So I wanted to hide these columns and I didn’t want to do this manually in the designer of the DataGridView for each form. I even didn’t want to know about the names of those fields/columns and write something against that. So the only way I could make this generic was by using the EF metadata of the Entity Object. But that’s where I was stuck… how could I get this information quickly?

    Well my solution uses Extension so I don’t have to type much code to get the metadata

    public static EntityType MetaData(this EntityObject entity, ObjectContext entityContext)
    {
    return entityContext.MetadataWorkspace.GetItemCollection(DataSpace.CSpace).GetItem(entity.GetType().FullName);
    }

    What this allows me to do is get the metadata of an entityobject instance like this

    var col = fooObjectEntity.MetaData(DataContainer.GetInstance().DataEntities).KeyMembers;

    One small problem with this solutions is that you have to pass along the ObjectContext.
    Some people solve this by adding something similar to the ObjectContext itself, take a look here ( it are some thoughts on a question Julie Lerman asked ).
    Passing the ObjectContext is needed, because it’s not possible to get it from the EntityObject itself, as talked about here! ( although I haven’t checked out Patrick’s version of a possible solution yet… look here )

  • Setting the bindingsource
  • I don’t know why, but for some reason I can’t just bind the ObjectQuery to the DataGridView.DataSource directly! Ok maybe wrong again, I can do this and the grid will show the data, but when I add a EntityObject to the given ObjectContext and do a .SaveChanges(), the grid will not refresh the data!! ( after the SaveChanges() I reset the DataSource of the grid to the ObjectQuery )
    I don’t have a clue why this is… updating data will work fine, but adding rows/objects won’t!
    The only solution I found, that solves this is to use Object instantiator of the ObjectQuery. In my case the .ToList() method.

    this.categoryBindingSource.DataSource = DataContainer.GetInstance().DataEntities.Category.ToList();

  • Using related data inside the DataGridView
  • Something that annoyed me was the fact that DataGridView didn’t pick up related data fields as columns.
    For example when you have some EntityObject Member that has a relationship to Address, doing following in code will not get the Address fields as columns inside the grid.

    this.memberBindingSource.DataSource = DataContainer.GetInstance().DataEntities.Member.Include("Address").ToList();

    A pity it doesn’t do this… because when I want to get a row back from the grid I still want to have a EntityObject of type Member, so writing some ObjectQuery myself with all the data seems not the good solution.
    For this little problem I asked what Julie Lerman would do. And she pointed out that the way she does it, is by inserting the values manually during the moment that the grid gets populated. Details are in the comments here. So the solution I used was getting hold of the CellFormatting event and pasting the needed values in the correct cell.

    private void gridSchool_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
    if (!this.gridSchool.Rows[e.RowIndex].IsNewRow && this.gridSchool.Columns[e.ColumnIndex].Name.Equals("addressStreet", StringComparison.OrdinalIgnoreCase))
    {
    School currentSchool = this.gridSchool.Rows[e.RowIndex].DataBoundItem as School;
    e.Value = currentSchool.Address.addressStreet;
    }
    }

Well that’s it for the moment… like you see most of the things I stumbled upon are also stuff other people noticed, so maybe they will get addressed during v2 of the Entity Framework ?
On the other hand I still do like the EF as a concept and I hope Microsoft will invest much time and effort into it :).

Technorati Tags: ,

Depechie

Leave a Reply

Your email address will not be published. Required fields are marked *