views:

100

answers:

1

So I have a model in my domain similar to this:

public class Product
{
    public virtual Tag Methodology { get; set; }
}

Then in an webform project I update it like so:

if (!string.IsNullOrWhiteSpace(ddlMethodology.SelectedValue))
    product.Methodology = TagRepo.GetTagById(int.Parse(ddlMethodology.SelectedValue));
else
    product.Methodology = null;

But this wasn't updating when product.Methodology was previously set to an object and I wanted to change it back to nothing. I.e. the product.Methodology = null; line didn't seem to work as expected.

When I ran it in the debugger I found that sometimes it would work and sometimes it wouldn't. After a small amount of hair pulling, I realised it was due to the proxy type that the entity framework was creating for that property at runtime and it was working when I inspected it in the debugger.

So to fix the issue, I created this hack which works well: (NOTE: now an else if)

if (!string.IsNullOrWhiteSpace(ddlMethodology.SelectedValue))
    product.Methodology = TagRepo.GetTagById(int.Parse(ddlMethodology.SelectedValue));
else if (product.Methodology != null)
    product.Methodology = null;

So I guess my questions are:

  • Am I doing something wrong?
  • Is there another way it can be done remembering to do a hack everytime?
  • Could it be considered a bug in the entity framework code first CTP?

Cheers,
Charles

A: 

I'm having the same kind of problem, but my entity will not save. I've tried to attach it to the context before calling save. but it does not seem to work.

Alexandre Brisebois