EF 4,如何添加部分类

发布于 2024-11-06 19:22:15 字数 4041 浏览 1 评论 0原文

我需要扩展我的 EF 部分类,因为我想添加一些功能以能够使用 Oracle 的序列,但是我真的不知道如何使用这个部分类,我制作了一个单独的 .cs 文件并将其命名为 one我的自动生成的类如下:

namespace GlassStoreDAL
{
    public partial class CAR 
    {
        private int _sequences;
        public int sequences
        {
            get { return _sequences; }
            set { _sequences = value; }
        }
    }  
}

现在我假设,在我的 BLL(引用 GlassStoreDAL)上,我可以找到我的“序列”属性,但显然出了问题,我将不胜感激。

这是我生成的部分类,我应该也有序列属性吗?

[EdmEntityTypeAttribute(NamespaceName="Model", Name="CAR")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class CAR : EntityObject
{
    #region Factory Method
    /// <summary>
    /// Create a new CAR object.
    /// </summary>
    /// <param name="id">Initial value of the ID property.</param>
    public static CAR CreateCAR(global::System.Decimal id)
    {
        CAR cAR = new CAR();
        cAR.ID = id;
        return cAR;
    }

    #endregion
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Decimal ID
    {
        get
        {
            return _ID;
        }
        set
        {
            if (_ID != value)
            {
                OnIDChanging(value);
                ReportPropertyChanging("ID");
                _ID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ID");
                OnIDChanged();
            }
        }
    }

    private global::System.Decimal _ID;
    partial void OnIDChanging(global::System.Decimal value);
    partial void OnIDChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String NAME
    {
        get
        {
            return _NAME;
        }
        set
        {
            OnNAMEChanging(value);
            ReportPropertyChanging("NAME");
            _NAME = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("NAME");
            OnNAMEChanged();
        }
    }

    private global::System.String _NAME;
    partial void OnNAMEChanging(global::System.String value);
    partial void OnNAMEChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String MODEL
    {
        get
        {
            return _MODEL;
        }
        set
        {
            OnMODELChanging(value);
            ReportPropertyChanging("MODEL");
            _MODEL = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("MODEL");
            OnMODELChanged();
        }
    }

    private global::System.String _MODEL;
    partial void OnMODELChanging(global::System.String value);
    partial void OnMODELChanged();

    #endregion

    #region Navigation Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("Model", 
        "SYS_C009618", "GLASS")]
    public EntityCollection<GLASS> GLASSes
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.
                GetRelatedCollection<GLASS>("Model.SYS_C009618", "GLASS");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.
                    InitializeRelatedCollection<GLASS>("Model.SYS_C009618", 
                        "GLASS", value);
            }
        }
    }

    #endregion
}

I needed to extend my EF partial classes, because I want to add some functionality to able to use Oracle's sequences , however I really don't know how to use this partial class thing, I made a seperate .cs file and name it as one of my auto-generated classes as follows:

namespace GlassStoreDAL
{
    public partial class CAR 
    {
        private int _sequences;
        public int sequences
        {
            get { return _sequences; }
            set { _sequences = value; }
        }
    }  
}

Now I assumed that, on my BLL - which references GlassStoreDAL - I can find my "sequences" property , but apparently something goes wrong, I would appreciate any help here.

Here is my generated partial class , should I have the sequences property also there?

[EdmEntityTypeAttribute(NamespaceName="Model", Name="CAR")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class CAR : EntityObject
{
    #region Factory Method
    /// <summary>
    /// Create a new CAR object.
    /// </summary>
    /// <param name="id">Initial value of the ID property.</param>
    public static CAR CreateCAR(global::System.Decimal id)
    {
        CAR cAR = new CAR();
        cAR.ID = id;
        return cAR;
    }

    #endregion
    #region Primitive Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
    [DataMemberAttribute()]
    public global::System.Decimal ID
    {
        get
        {
            return _ID;
        }
        set
        {
            if (_ID != value)
            {
                OnIDChanging(value);
                ReportPropertyChanging("ID");
                _ID = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("ID");
                OnIDChanged();
            }
        }
    }

    private global::System.Decimal _ID;
    partial void OnIDChanging(global::System.Decimal value);
    partial void OnIDChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String NAME
    {
        get
        {
            return _NAME;
        }
        set
        {
            OnNAMEChanging(value);
            ReportPropertyChanging("NAME");
            _NAME = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("NAME");
            OnNAMEChanged();
        }
    }

    private global::System.String _NAME;
    partial void OnNAMEChanging(global::System.String value);
    partial void OnNAMEChanged();

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String MODEL
    {
        get
        {
            return _MODEL;
        }
        set
        {
            OnMODELChanging(value);
            ReportPropertyChanging("MODEL");
            _MODEL = StructuralObject.SetValidValue(value, true);
            ReportPropertyChanged("MODEL");
            OnMODELChanged();
        }
    }

    private global::System.String _MODEL;
    partial void OnMODELChanging(global::System.String value);
    partial void OnMODELChanged();

    #endregion

    #region Navigation Properties

    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [XmlIgnoreAttribute()]
    [SoapIgnoreAttribute()]
    [DataMemberAttribute()]
    [EdmRelationshipNavigationPropertyAttribute("Model", 
        "SYS_C009618", "GLASS")]
    public EntityCollection<GLASS> GLASSes
    {
        get
        {
            return ((IEntityWithRelationships)this).RelationshipManager.
                GetRelatedCollection<GLASS>("Model.SYS_C009618", "GLASS");
        }
        set
        {
            if ((value != null))
            {
                ((IEntityWithRelationships)this).RelationshipManager.
                    InitializeRelatedCollection<GLASS>("Model.SYS_C009618", 
                        "GLASS", value);
            }
        }
    }

    #endregion
}

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

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

发布评论

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

评论(3

热鲨 2024-11-13 19:22:15

总结大量的注释线索...

检查部分是否正确附加在一起:

  • 确保两个类定义位于相同的命名空间和程序集中。
  • 确保其中至少一个被声明为部分类(大多数生成的类都是如此,包括 EF 生成的类)。
  • 检查以确保新创建的部分可以看到以前的成员,以确认部分匹配。

如果客户端位于不同的二进制文件中(这里就是这种情况),

  • 请确保客户端项目二进制文件/引用是最新的(执行干净的构建/删除二进制副本/重新创建引用),具体取决于您的项目情况。

对于这种情况,最后一次检查是最重要的,解决了问题。

To summarise the large comment trail...

Check that the partials are being attached together correctly:

  • Make sure that both class definitions are in the same namespace and assembly.
  • Make sure at least one of them is declared as partial (most generated classes are, including EF generated ones).
  • Check to make sure that the newly created partial can see the previous members, to confirm the partials match up.

Where the client is in a different binary (which was the case here)

  • Make sure the client projects binary/references are up to date (perform a clean build / delete the binary copy / recreate the reference), depending upon your project situation.

For this case, the last check was the most important and solved the problem.

痞味浪人 2024-11-13 19:22:15

您应该确保:

public partial class CAR 
{
    private int _sequences;
    public int sequences
    {
        get { return _sequences; }
        set { _sequences = value; }
    }
}

在生成的 EF 类中,您需要:

public partial class CAR 
{
}  
  1. 将partial 关键字添加到 EF 生成的类中。
  2. 确保它们驻留在同一名称空间中。

You should make sure that:

public partial class CAR 
{
    private int _sequences;
    public int sequences
    {
        get { return _sequences; }
        set { _sequences = value; }
    }
}

In your generated EF class you are required to:

public partial class CAR 
{
}  
  1. Add partial keyword to the EF generated class.
  2. Make sure they reside in the same namespace.
梦纸 2024-11-13 19:22:15

在同一程序集中的单独文件中创建一个新类(尽管它不必是同一程序集),并确保它具有相同的命名空间。

如果它们都位于相同的程序集和命名空间中,那么您应该不会遇到任何问题。当您创建的新部分可以在源代码编辑器顶部的下拉列表中看到生成的 EF 类的属性和方法时,您就会知道您已经做对了。

Create a new class in a separate file in the same assembly (although it doesn't have to be the same assembly) and make sure it has the same namespace.

If they are both in the same assembly and namespace you shouldn't have any issues. You'll know that you've got it right when the new partial you've created can see the properties and methods of the generated EF class in the dropdown at the top of the source code editor.

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