Well obviously the code does have two if statements, but again, I don't see any reasoning behind why the template is written to
NOT allow default values if both of those properties are set to true.
In short I needed default values and event notifications. I removed the check and added a bunch of additional bits to prevent premature firing of events. If any one is interested continue on.
You could use and set an
IsInitialized variable at the end of the constructor for each entity to achieve this, which works just fine, but a much better way is to set a variable like this from your DbContext's
ObjectMaterialized event. This way the notifications won't fire until the EF is finished setting the values and only changes that occur in your own code trigger notifications.
This isn't everything but the basics are as follows. At some point I'll post a new template because there is a new
INotification interface I'm working on that uses lambda expressions from start to finish that should be in the BCL in the first place. string events = fail.
Template entity additions:
Every entity needs these bits.
Code: Select all
private bool _isInitialized = false;
/// <summary>
/// Returns true if the class has been initialized; false otherwise. Used to prevent premature firing of event notifications.
/// </summary>
[NotMapped]
public bool IsInitialized { get { return _isInitialized; } set { _isInitialized = value; } }
Add these extra bits to the sections dealing with firing events:
Code: Select all
if(IsInitialized)
PropertyChanging(...);
...
if(IsInitialized)
PropertyChanged(...);
This
Initialize method would need to be fired from the constructor of the DbContext.
Code: Select all
public partial class MyDbContext
{
partial void Initialize()
{
((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += ObjectContext_ObjectMaterialized;
}
void ObjectContext_ObjectMaterialized(object sender, System.Data.Objects.ObjectMaterializedEventArgs e)
{
/*
* set the IsInitialized property for every materialized entity to true.
* this allows INotification changes to fire for property changes in each entity
*/
var entity = e.Entity as IEntity;
if (entity != null)
entity.IsInitialized = true;
}
}
IEntity is just a simple interface that so we can change the
IsInitialized variable. Modify the template to have each entity implement the interface.
Code: Select all
public interface IEntity
{
bool IsInitialized { get; set; }
}