我有一个具有以下结构的Hierarchical表
如您所见,ParentGroupUserID引用同一个表。现在当我使用EF POCO t4模板为它生成类时,它会产生以下结构。
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
}
如果你在上面的代码中看到,我不喜欢命名我的公共属性GroupUser1(它是一个集合)和GroupUser2(单个对象)的概念。谁能帮助我理解POCO生成的这种命名模式,有没有办法可以重命名它(不修改t4模板)。
答案 0 :(得分:1)
我认为EF t4 POCO模板不是为处理这种情况而设计的。手动编辑文件不是一个好主意,因为下次生成POCO时您将丢失更改。
所以你不得不修改模板。但这并不是那么糟糕,因为你可以修改模板,这样如果表和字段与你上面给出的那些匹配,它只有不同的输出。
例如,在我的t4模板中,我添加了这个:
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]
<# } #>
此修改允许我将[Required]标志添加到数据库中具有NOT NULL列的每个字段,除了提到的两个字段(未设置为必需,因为UI不允许您输入这些字段) 。
对于某些与常规不同的字段,很容易有特定的规则。
答案 1 :(得分:1)
这不是POCO生成的命名模式。这些名称位于您的实体模型中。更改实体模型中的名称(EDMX文件 - 设计器),它们也将在生成的代码中更改。