Entity Framework POCO template for Hierarchical table
I got a Hierarchical table with following structure
As you can see, ParentGroupUserID refers to the same table. Now whe开发者_开发技巧n I use EF POCO t4 template to generate class for it, it comes up with following structure.
public partial class GroupUser
{
#region Primitive Properties
public virtual int GroupUserID
{
get;
set;
}
public virtual int GroupID
{
get;
set;
}
public virtual string UserName
{
get;
set;
}
#endregion
#region Navigation Properties
public virtual ICollection<GroupUser> GroupUser1
{
get
{
if (_groupUser1 == null)
{
var newCollection = new FixupCollection<GroupUser>();
newCollection.CollectionChanged += FixupGroupUser1;
_groupUser1 = newCollection;
}
return _groupUser1;
}
set
{
if (!ReferenceEquals(_groupUser1, value))
{
var previousValue = _groupUser1 as FixupCollection<GroupUser>;
if (previousValue != null)
{
previousValue.CollectionChanged -= FixupGroupUser1;
}
_groupUser1 = value;
var newValue = value as FixupCollection<GroupUser>;
if (newValue != null)
{
newValue.CollectionChanged += FixupGroupUser1;
}
}
}
}
private ICollection<GroupUser> _groupUser1;
public virtual GroupUser GroupUser2
{
get { return _groupUser2; }
set
{
if (!ReferenceEquals(_groupUser2, value))
{
var previousValue = _groupUser2;
_groupUser2 = value;
FixupGroupUser2(previousValue);
}
}
}
private GroupUser _groupUser2;
public virtual ICollection<Franchise> Franchises
{
get
{
if (_franchises == null)
{
var newCollection = new FixupCollection<Franchise>();
newCollection.CollectionChanged += FixupFranchises;
_franchises = newCollection;
}
return _franchises;
}
set
{
if (!ReferenceEquals(_franchises, value))
{
var previousValue = _franchises as FixupCollection<Franchise>;
if (previousValue != null)
{
previousValue.CollectionChanged -= FixupFranchises;
}
_franchises = value;
var newValue = value as FixupCollection<Franchise>;
if (newValue != null)
{
newValue.CollectionChanged += FixupFranchises;
}
}
}
}
private ICollection<Franchise> _franchises;
#endregion
#region Association Fixup
private void FixupGroupUser2(GroupUser previousValue)
{
if (previousValue != null && previousValue.GroupUser1.Contains(this))
{
previousValue.GroupUser1.Remove(this);
}
if (GroupUser2 != null)
{
if (!GroupUser2.GroupUser1.Contains(this))
{
GroupUser2.GroupUser1.Add(this);
}
}
}
private void FixupGroupUser1(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (GroupUser item in e.NewItems)
{
item.GroupUser2 = this;
}
}
if (e.OldItems != null)
{
foreach (GroupUser item in e.OldItems)
{
if (ReferenceEquals(item.GroupUser2, this))
{
item.GroupUser2 = null;
}
}
}
}
private void FixupFranchises(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (Franchise item in e.NewItems)
{
if (!item.GroupUsers.Contains(this))
{
item.GroupUsers.Add(this);
}
}
}
if (e.OldItems != null)
{
foreach (Franchise item in e.OldItems)
{
if (item.GroupUsers.Contains(this))
{
item.GroupUsers.Remove(this);
}
}
}
}
#endregion
}
If you see in the above code, I don't like the notion of naming my public properties GroupUser1 (which is a collection) and GroupUser2 (single object). Can anyone help me understand this naming pattern with POCO generation and is there a way I can rename it (without modifying the t4 template).
That is not naming pattern of POCO generation. These names are in your entity model. Change names in entity model (EDMX file - designer) and they will be changed in generated code as well.
I don't think that the EF t4 POCO template was designed to handle that scenario. It's not a good idea to manually edit the file, since you will lose your changes the next time you generate the POCOs.
So you are left with having to modify the template. But that's not so bad, because you can modify the template so that it only has different output if the table and field match the ones you gave above.
For example in my t4 template I have added this:
bool isRequired = !edmProperty.Nullable;
if (isRequired && entity.Name == "Adjustment" && propertyName == "AdjustmentNumber")
{
isRequired = false;
}
if (!isRequired && entity.Name == "Order" && propertyName == "RequiredDate")
{
isRequired = true;
}
<# if (isRequired) { #> [Required]
<# } #>
This modification allows me to add the [Required] flag to every field that has a NOT NULL column in the database, except for the two fields mentioned (not set to required because the UI does not allow you to enter these fields).
Its quite easy to have specific rules for certain fields that are in someway different from the norm.
精彩评论