层次表的实体框架POCO模板
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I got a Hierarchical table with following structure enter image description here

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).

#0

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.

#1

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.

推荐文章

WPF:ScrollViewer上的滚动条未激活

WPF:ScrollViewer上的滚动条未激活

推荐文章

skybound的GeckoWebBrowser在运行https站点时给出“连接不可信”

skybound的GeckoWebBrowser在运行https站点时给出“连接不可信”

推荐文章

关于3p20库中的修改

关于3p20库中的修改

推荐文章

在Tomcat 7.0服务器上使用Ajax和JSF时遇到问题

在Tomcat 7.0服务器上使用Ajax和JSF时遇到问题

推荐文章

当使用Apache Httpclient时,是否有方法获得下载状态?

当使用Apache Httpclient时,是否有方法获得下载状态?

推荐文章

如何在GAE上导入MySQLdb模块?

如何在GAE上导入MySQLdb模块?

推荐文章

类型转换为EntityCollection

类型转换为EntityCollection

推荐文章

pixel.gif,为什么人们使用它?

pixel.gif,为什么人们使用它?

推荐文章

如何使每次按键都更改图像?

如何使每次按键都更改图像?

推荐文章

extjs网格发布数据轨道

extjs网格发布数据轨道

推荐文章

比较两个列表

比较两个列表

推荐文章

如何在c#方法中将decimal作为参数发送

如何在c#方法中将decimal作为参数发送

推荐文章

谁可以在Linux/UNIX上访问具有八进制权限“000”的文件?

谁可以在Linux/UNIX上访问具有八进制权限“000”的文件?

推荐文章

ado.net如何管理事务

ado.net如何管理事务

推荐文章

Java中是否有用于散列的hexToString实现方法?

Java中是否有用于散列的hexToString实现方法?

推荐文章

带多个参数的ActionLink

带多个参数的ActionLink