I’m having an entity like that:
public class Part : Entity
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<VersionSection> VersionSections
{
get
{
return Sections.Where(s => s is VersionSection).Cast<VersionSection>();
}
}
public virtual ICollection<Section> Sections { get; set; }
public Part()
{
this.Sections = new List<Section>();
}
}
I would like to set the default value for the Sections collection very time when I create a new instance of Part following to this business:
- When creating a Part, a default Section (Name = “Section 1”) should be created. This cannot be deleted.
There’s no problem on creating a new one, but when getting data from DB, EF create a default instance of Section and also add the data from DB to my entity, so it’s wrong.
Any ideas? Thanks
There is no fool proof way to achieve what you need at the time of entity creation. However you can do this before the entity gets saved.