分层表的实体框架 POCO 模板
我得到了一个具有以下结构的层次结构表
正如您所看到的,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 模板)。
I got a Hierarchical table with following structure
As you can see, ParentGroupUserID refers to the same table. Now when 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是 POCO 一代的命名模式。这些名称位于您的实体模型中。更改实体模型(EDMX 文件 - 设计器)中的名称,它们也会在生成的代码中更改。
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.
我不认为 EF t4 POCO 模板是为处理这种情况而设计的。手动编辑该文件不是一个好主意,因为下次生成 POCO 时您将丢失所做的更改。
因此您必须修改模板。但这还不错,因为您可以修改模板,以便只有当表和字段与上面给出的匹配时它才会有不同的输出。
例如,在我的 t4 模板中,我添加了以下内容:
此修改允许我将 [Required] 标志添加到数据库中具有 NOT NULL 列的每个字段,除了提到的两个字段(未设置为必需,因为 UI 不不允许您输入这些字段)。
对于某些领域制定与规范有所不同的特定规则是很容易的。
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:
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.