Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - EF 4, how to add partial classes

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...