6

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
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
Musaab
  • 805
  • 2
  • 13
  • 30
  • Are both partials in the same assembly? – forsvarir May 11 '11 at 09:47
  • yes , if u mean by the same assembly , the same DLL project – Musaab May 11 '11 at 09:49
  • Both classes are declared as partial / in the same namespace? – forsvarir May 11 '11 at 09:59
  • No you shouldn't have the sequences property declared there as well... that would make partials largely pointless... what namespace is the generated class in? – forsvarir May 11 '11 at 10:04
  • please see my edited post , I attached the auto-generated partial class – Musaab May 11 '11 at 10:06
  • yeah it is the same "GlassStoreDAL" namespace – Musaab May 11 '11 at 10:07
  • @Musaab: The code you've included doesn't include a namespace for the generated class... If that's the case in the file, then you should remove the namespace wrapper around *your* partial class to make sure they're in the same namespace. Otherwise they're treated like different classes. – forsvarir May 11 '11 at 10:08
  • yeah I confirm it is the same namespace, beside it seems like the problem in my implementation on the BLL , because on my custom partial class I can see properties other than "sequences" showed by the intellisense. – Musaab May 11 '11 at 10:15
  • @Musaab: So you are doing something like: `GlassStoreDAL.CAR newCar = new CAR(); newCar.sequences = 5;` ... What error are you getting when you compile? – forsvarir May 11 '11 at 10:26
  • Error 1 'GlassStoreDAL.CAR' does not contain a definition for 'sequences' and no extension method 'sequences' accepting a first argument of type 'GlassStoreDAL.CAR' could be found (are you missing a using directive or an assembly reference?) – Musaab May 11 '11 at 10:34
  • Your client is in the same assembly/dll as the CAR classes? If you add the line `NAME="test";` into your sequences `set` function, does it still compile (reference one of the properties from the other half of the partial)? – forsvarir May 11 '11 at 10:42
  • no my BLL is on different DLL project , yeah I insert that line into the set function of my sequence property and it does compile successfully, all good , pretty confusing ha ? – Musaab May 11 '11 at 10:51
  • **no my BLL is on different DLL project** How are you referencing the project you've added the partial class to? Are you sure the binary is up to date (try a clean build / deleting the binary) to make sure... – forsvarir May 11 '11 at 10:54
  • @forsvarir I guess I'm going to change my whole plan , I needed this because I wanted to have auto increment column on my database , I will change that and just do a custom generation IDs method , I appreciate your time , thanks – Musaab May 11 '11 at 10:55
  • wow @forsvarir , I did exactly what you said as a final move , I removed the reference and add it again after a clean build , and yeah it finally worked out, thanks alot – Musaab May 11 '11 at 11:16

3 Answers3

9

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.

forsvarir
  • 10,749
  • 6
  • 46
  • 77
  • Considering this comment can be just comments and not an actual answer, with just typo error. Considering error is this, with a genius of clicking the down button. – Peyton Crow May 12 '11 at 03:02
  • 1
    @Peyton Crow: That's pretty ridiculous considering he solved the problem. – razlebe May 12 '11 at 12:29
6

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.
Peyton Crow
  • 872
  • 4
  • 9
  • 1
    @Peyton Crow: You should remove the sequences information from your *generated* EF class... it makes it look like you have to reimplement the property in the generated code... – forsvarir May 11 '11 at 09:55
  • Just removed it just now, thanks for the reminder, its a bit confusing as an answer. – Peyton Crow May 12 '11 at 01:41
  • @Peyton Crow: I've removed my -1, now that you've removed the misleading code. – forsvarir May 12 '11 at 05:03
  • @Peyton Crow: I'll usually comment when I leave an answer downvote and come back and remove it if I get a message to say it's been fixed (which is what happened here). You can't change your vote on my answer (I think you get 5 mins after you cast the vote), unless I, or somebody else edits it, which at the moment I've got no need to. A downvote's not that big a deal and each person votes using their own approach... :) – forsvarir May 12 '11 at 05:20
  • 4
    @Peyton Crow - Don't downvote someone else's answer just because they downvoted yours. It's petty. Learn to take constructive criticism. – Kirk Broadhurst May 12 '11 at 06:38
  • 1
    @Peyton Crow - Also "1.Add partial keyword to the EF generated class." - the `partial` keyword should already exist there. It's not recommended to edit generated code. – Kirk Broadhurst May 12 '11 at 06:39
1

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.

Darren Lewis
  • 8,338
  • 3
  • 35
  • 55
  • oh yeah thats right , I can see other properties in the editor , the problem must be then in how I'm trying to reference it on my BLL – Musaab May 11 '11 at 10:11
  • GlassStoreDAL.CAR newCar = new CAR(); newCar. this is how I'm doing it on my BLL , I can see all the properties but the "sequences" one – Musaab May 11 '11 at 10:16
  • @Musaab: stupid question, but you *are* sure it's not there and it's not just that intellisense hasn't caught up yet? – forsvarir May 11 '11 at 10:18