實體框架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模板)。
太棒了。我沒有這樣想。你真的睡拉迪斯拉夫嗎?對於任何EF問題,我通常會得到即時答案(甚至可以提供)。感謝您的回答 – DotNetInfo 2011-05-14 11:36:50