分层表的实体框架 POCO 模板

发布于 2024-11-07 01:21:16 字数 4770 浏览 0 评论 0原文

我得到了一个具有以下结构的层次结构表 在此处输入图像描述

正如您所看到的,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
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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜司空 2024-11-14 01:21:16

这不是 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.

岁月流歌 2024-11-14 01:21:16

我不认为 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 不不允许您输入这些字段)。

对于某些领域制定与规范有所不同的特定规则是很容易的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文