oracle EF 4、如何添加分部类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5962235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:38:26  来源:igfitidea点击:

EF 4, how to add partial classes

c#.netoracleentity-framework-4partial-classes

提问by Musaab

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:

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

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.

现在我假设,在我的 BLL 上 - 引用 GlassStoreDAL - 我可以找到我的“序列”属性,但显然出了点问题,我很感激这里的任何帮助。

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
}

回答by forsvarir

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.
  • 确保两个类定义在相同的命名空间和程序集中。
  • 确保其中至少一个被声明为部分(大多数生成的类,包括 EF 生成的类)。
  • 检查以确保新创建的部分可以看到以前的成员,以确认部分匹配。

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.

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

回答by Peyton Crow

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:

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

public partial class CAR 
{
}  
  1. Add partial keyword to the EF generated class.
  2. Make sure they reside in the same namespace.
  1. 向 EF 生成的类添加部分关键字。
  2. 确保它们位于相同的命名空间中。

回答by Darren Lewis

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.

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