The problem is this: (there's a solution,
- with a single entity, the entity is instantiated, gets an empty EntityFields2 instance, OnInitialized is called. Then afterwards you fetch it, values are overwritten, everything is fine.
- with collection fetches (it also goes wrong if you simply fetch a collection), the datareader is traversed, every row is fetched into an EntityFields2 instance and then passed to a new instance of the entity, through the CTor.
This means that although 'IsNew' is true (it's set to false right after that) you will overwrite fetched data.
So do:
public partial class TemplateFieldEntity
{
protected override void OnInitialized()
{
base.OnInitialized();
if((this.Fields!=null) && (this.Fields.State!=EntityState.Fetched))
{
IsEnabled = true;
}
}
}
this works, because it only sets the value if the state isn't fetched which is when the entity is new.