locked
How to handle LookUp tables with Nullable FK's RRS feed

  • Question

  • Hello,

    how is the right (correct) way to handle LookUp Tables with Entity Framework (EF) and WCF Data Services?

    For example:
    Image the usally entity of a customer. This customer has a name, a lastname and e.g. a title (Dr., Prof., ...)
    Am I wrong to set up the database table with Nullable Field for the title if the customer has no title?
    I did so because of normalisation, or?

    Now I have to visualize the customer in a WinForm-App. For the lookup of titel I want to use a comboBox.
    So if I retrieve from my Service an IList<Title> to set it as datasource. Works fine but I can't deal the null value for "none Title".

    Ok. So populate the comboBox myself:

    ComboBox.Items.Add("<none>");
    
    foreach (Title t in TitleSet){
    
       ComboBox.Items.add(t);
    
    }
    
    
    
    ComboBox.DisplayMember="description";
    
    ComboBox.ValueMember="id";
    
    
    Ok works.

    But now how to the dataBinding?

    Binding newBinding = new Binding("selectedItem", bindingSource1, "Titel");
    
    newBinding.FormattingEnabled = true;
    
    newBinding.NullValue = "<none>";
    
    newBinding.DataSourceNullValue = null;
    
    newBinding.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation;
    
    comboBox.DataBindings.Add(newBinding);
    
    
    Show Up only correctly the Null-Value

    So I tried with "selectedValue":

    This Only show the title if ones set correctly...

    So please: Where is my mistake?

    Thanks in advance

    Bernhard


    • Edited by OrgMulle Monday, March 8, 2010 11:06 AM Code corrected
    Monday, March 8, 2010 11:06 AM

Answers

  • Hi ,

    You can use the projections feature in the client library to perform the check for a null value in the Title field.

    an example is :

    var customersSet = from customer in client.CreateQuery<Customer>("Customers")
    select new { Title = customer.Title.Length > 0 ? customer.Title : "<none>" };
    
    

     

     

    This way , the client library performs the replacement for you and you can just bind the resulting collection to the Combobox.


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj

     

    Wednesday, April 7, 2010 5:46 PM
    Moderator

All replies

  • Hi together,

    is this the right way of doing it? At least it seems to work ;-)

    this.TitelList = value;
    Titel noneTitle = new Titel();
    noneTitle.description = "<none>";
    
    this.TitelList.Insert(0, noneTitle);                 
    this.comboBox.DataSource = this.TitelList;
    this.comboBox.DisplayMember = "description";               
    this.comboBox.ValueMember = "id";
    All the best,

    Bernhard
    Monday, March 8, 2010 12:55 PM
  • Hi ,

    You can use the projections feature in the client library to perform the check for a null value in the Title field.

    an example is :

    var customersSet = from customer in client.CreateQuery<Customer>("Customers")
    select new { Title = customer.Title.Length > 0 ? customer.Title : "<none>" };
    
    

     

     

    This way , the client library performs the replacement for you and you can just bind the resulting collection to the Combobox.


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj

     

    Wednesday, April 7, 2010 5:46 PM
    Moderator