Create an Entity from an EntitytCollection...

Posts   
 
    
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 03-May-2005 01:58:30   

Here's my code...


//lookup the user entity 
//this part works...
EntityCollection userCollection = new EntityCollection(new ExecutempsSW.Data.Base.FactoryClasses.UsersEntityFactory());
RelationPredicateBucket userPredicateBucket = new RelationPredicateBucket();
IPredicateExpression A = new PredicateExpression();
A.Add(ExecutempsSW.Data.Base.FactoryClasses.PredicateFactory.CompareValue(UsersFieldIndex.WinLogon, ComparisonOperator.Equal, UserName));
A.AddWithAnd(ExecutempsSW.Data.Base.FactoryClasses.PredicateFactory.CompareValue(UsersFieldIndex.WinDomain, ComparisonOperator.Equal, UserDomain));
userPredicateBucket.PredicateExpression.Add(A);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(userCollection, userPredicateBucket);

// get the UsersEntity
if (userCollection.Count == 1)
{
      //heres where it blows...
      UsersEntity myUserEntity = (UsersEntity)userCollection[0];
      //same results with implicit or explicit casting
}

Cannot implicitly convert type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntity2' to 'ExecutempsSW.Data.Base.EntityClasses.UsersEntity'

Obviously I don't know what I'm doing... can you provide some direction?

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 03-May-2005 10:20:43   

As you define the complete namespace name for the usersentityfactory class, could it be some sort of reference problem? or that the wrong UsersEntity type is referenced? (perhaps a silly question, but the code looks ok).

In the debugger, what's inside the collection, what type?

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 04-May-2005 01:09:16   

ok... I worked with it a bit and got it working per the previous message... now I need to cast the UsersEntity to a class that is inherited from itelf... perhaps best to explain in code:


//lookup the user entity 
ExecutempsSW.Data.Base.HelperClasses.EntityCollection userCollection = new EntityCollection( new ExecutempsSW.Data.Base.FactoryClasses.UsersEntityFactory());
SD.LLBLGen.Pro.ORMSupportClasses.RelationPredicateBucket userPredicateBucket = new RelationPredicateBucket();
SD.LLBLGen.Pro.ORMSupportClasses.IPredicateExpression A = new PredicateExpression();
A.Add(ExecutempsSW.Data.Base.FactoryClasses.PredicateFactory.CompareValue(UsersFieldIndex.Name, ComparisonOperator.Equal, UserName));
A.AddWithAnd(ExecutempsSW.Data.Base.FactoryClasses.PredicateFactory.CompareValue(UsersFieldIndex.Password, ComparisonOperator.Equal, Password));
userPredicateBucket.PredicateExpression.Add(A);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(userCollection, userPredicateBucket);
// get the UsersEntity
    if (userCollection.Count == 1)
    {
        //the goal here is to load the data into my ServerUser object,
        //which inherits from User,
        //which inherits from UsersEntity
        //I have specified the full 

        //this now works..  UsersEntity is the LLBLGen generic class
        //I'm not sure what the issue was, but it works now, so I made progress.  Thank you.
        ExecutempsSW.Data.Base.EntityClasses.UsersEntity myUserEntity = (ExecutempsSW.Data.Base.EntityClasses.UsersEntity)userCollection[0];
        //now it blows here.  User is my class that inherits from UsersEntity.
        //public class User: ExecutempsSW.Data.Base.EntityClasses.UsersEntity, SD.LLBLGen.Pro.ORMSupportClasses.IEntity2
        //I added the reference to IEntity2 in an attempt to get it to work.
        //That made no difference
        DotNetDesigns.Windows.Service.User.User myUser = (DotNetDesigns.Windows.Service.User.User)myUserEntity;
        //The next step...  ServerUser is my class that inherits from User.
        //for now I exception out before getting here
        return LoginUser((DotNetDesigns.Windows.Service.User.ServerUser)myUser, SessionID);
    }

This errors with an Invalid cast when casting from the generated UsersEntity to User (my inheritied class)

Any feedback is appreciated.

Thanks, Darwin

Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 04-May-2005 02:24:00   

I found the issue... I used csome copy paste techniques to build my User class (the one theat inherits from UsersEntity). This resulted in accidentally overriding some apparently important properties/metods of the base class.... oops.... I'll test more tomorrow, but I think I found my mistake.

Still, any feedback you can give me based on the code you are seeing will be appreciated. Do you see any apparent ways that I could improve on how I am doing things?

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 04-May-2005 12:00:55   

Darwin wrote:

I found the issue... I used csome copy paste techniques to build my User class (the one theat inherits from UsersEntity). This resulted in accidentally overriding some apparently important properties/metods of the base class.... oops.... I'll test more tomorrow, but I think I found my mistake.

Yeah, be sure that if you override something, you call the base class' version simple_smile

Still, any feedback you can give me based on the code you are seeing will be appreciated. Do you see any apparent ways that I could improve on how I am doing things?

Well, one thing you could do is to use more using statements at the top of your file, so the code looks more cleaner and you don't have to do a lot of typing simple_smile . Now the types are very long, which can degrade readability. I know I used 'A' and 'B' in the documentation to describe predicates, which was to illustrate it in a more abstract way, I'd consider more descriptive names wink

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 04-May-2005 23:57:53   

I was wrong about the solution to my casting issue... it is not resolved... the excess code that I found in my inheritied class did resolve another issue (not mentioned here, but I thought it was related). so.. this still does not work:


//lookup the user entity 
EntityCollection userCollection = new EntityCollection( new UsersEntityFactory());
RelationPredicateBucket userPredicateBucket = new RelationPredicateBucket();
IPredicateExpression A = new PredicateExpression();
A.Add(PredicateFactory.CompareValue(UsersFieldIndex.Name, ComparisonOperator.Equal, UserName));
A.AddWithAnd(PredicateFactory.CompareValue(UsersFieldIndex.Password, ComparisonOperator.Equal, Password));
userPredicateBucket.PredicateExpression.Add(A);
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.FetchEntityCollection(userCollection, userPredicateBucket);
// get the UsersEntity
if (userCollection.Count == 1)
    {
    //the goal here is to load the data into my ServerUser object,
    //which inherits from User,
    //which inherits from UsersEntity
    
    //this works..  UsersEntity is the LLBLGen generic class
    UsersEntity myUsersEntity = (UsersEntity)userCollection[0];

    //it blows here.  User is my class that inherits from UsersEntity.
    //this throws a runtime error of "Invalid Cast"
    User myUser = (User)myUserEntity;

    //The next step...  ServerUser is my class that inherits from User.
    //for now I exception out before getting here
    ServerUser myServerUser = (ServerUser)myUser;

    //this is a workaround...  extract the ID and refetch the data into a ServerUser
    //ServerUser myServerUser = new ServerUser(myUsersEntity.UsersID);
    return LoginUser(myServerUser, SessionID);

here is my Users Class:


using System;
using System.Runtime.Serialization;
using ExecutempsSW.Data.Base.EntityClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace DotNetDesigns.Windows.Service.User
{
    /// <summary>
    /// Summary description for User.
    /// </summary>
    public class User: UsersEntity
    {
        public User():base() 
        {
        }
        public User(int UserID): base(UserID)
        {
        }
        public User(int UserID, string UserKey, string SessionID):base(UserID)
        {
        }
        public User(IEntityFields2 fields):base(fields) 
        {
        }
        public User(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
        public virtual bool Save()
        {
            return false;
        }
        public virtual bool FetchData()
        {
            return false;
        }
        public virtual UserSession Login()
        {
            return new UserSession(0);
        }
        public virtual bool LoadDataset(UserDataset dsUser)
        {
            return false;
        }
        public UserDataset GetDataset()
        {
            return new UserDataset();
        }
        public virtual bool Logout()
        {
            return true;
        }
        public virtual bool ResetPassword()
        {
            return false;
        }
        public virtual bool ChangePassword(string sCurrentPassword, string sNewPassword, string sSessionID)
        {
            return false;
        }
        public override string SecretAnswer
        {
            get
            {
                return "";
            }
        }
        public override string Password
        {
            get
            {
                return "";
            }
        }
        public override string SecretQuestion
        {
            get
            {
                return "";
            }
        }
    }
}

and my ServerUser class:


using System;
using DotNetDesigns.Windows.Service.User;
using ExecutempsSW.Data.Base.DatabaseSpecific;
using ExecutempsSW.Data.Base;
using ExecutempsSW.Data.Base.EntityClasses;
using ExecutempsSW.Data.Base.FactoryClasses;
using SD.LLBLGen.Pro.DQE;
using System.Runtime.Serialization;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace DotNetDesigns.Windows.Service.User
{
    /// <summary>
    /// Summary description for ServerUser.
    /// </summary>
    public class ServerUser: User
    {
        public ServerUser():base()
        {
        }
        public ServerUser(int UserID):base(UserID)
        {
        }
        public ServerUser(int UserID, string UserKey, string SessionID) :base(UserID, UserKey, SessionID)
        {
            this.FetchData();
        }
        public ServerUser(IEntityFields2 fields):base(fields)   
        {
        }
        public ServerUser(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
        public override bool Save()
        {
            DataAccessAdapter adapter = new DataAccessAdapter();
            return adapter.SaveEntity(this, true);
        }
        public override bool FetchData()
        {
            DataAccessAdapter adapter = new DataAccessAdapter(true);
            bool results = adapter.FetchEntity(this);
            //this is another thing I'm trying to figure out... I'll leave it in a seperate thread...
            //adapter.FetchEntity(this.UsersKey);
            //adapter.FetchEntity(this.UsersContact);
            //this.UsersContact = (ContactEntity)adapter.FetchNewEntity(new ContactEntityFactory(), this.GetRelationInfoUsersContact());
            adapter.CloseConnection();
            return results;
        }
        public override UserSession Login()
        {
            DataAccessAdapter adapter = new DataAccessAdapter();
            this.FetchData();

            //check to see if a key exists
            string myKeyString = this.UsersKey.IsNew.ToString();
            if(myKeyString == "")
            {
                //if no key exists, create one
                UsersKeyEntity myKey = new UsersKeyEntity(this.UsersID);
            }
            //UserSession mySession = new UserSession(SessionID);
            //add new UsersSession to key
            //Save
            //return adapter.SaveEntity(myKey, true);
            return new UserSession();
        }
        public override bool LoadDataset(UserDataset dsUser)
        {
            return new UserDataset();
        }

        public override bool Logout()
        {
            return true;
        }
        public override bool ResetPassword()
        {
            return true;
        }
        public override bool ChangePassword(string sCurrentPassword, string sNewPassword, string sSessionID)
        {
            return true;
        }
    }
}

Any idea why I can't cast the UsersEntity to a User? I expect the same issues when I try to cast a User to a ServerUser. Once I get the first cast done I will test the next one...

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 05-May-2005 11:56:41   

The original error occurs apparently because the UsersEntity is derived from a class which doesn't implement IEntity2.

I can only guess now, as I don't have / see the UsersEntity class, but could it be you accidently picked the wrong generator config and UsersEntity derives from EntityBase, not EntityBase2 (i.e. selfservicing entity) ?

Could paste the header (with the constructors, the rest I don't need) of the UsersEntity class declaration please?

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 05-May-2005 18:14:25   

Here's the header and ctors for UsersEntity:

 // Code is generated using LLBLGen Pro version: 1.0.2004.1
// Code is generated on: Thursday, April 14, 2005 12:05:10 PM
// Code is generated using templates: C# template set for SqlServer (1.0.2004.1)
// Templates vendor: Solutions Design.
// Templates version: 1.0.2004.1.102604
//////////////////////////////////////////////////////////////
using System;
using System.ComponentModel;
using System.Collections;
using System.Runtime.Serialization;

using ExecutempsSW.Data.Base;
using ExecutempsSW.Data.Base.HelperClasses;
using ExecutempsSW.Data.Base.FactoryClasses;
using ExecutempsSW.Data.Base.RelationClasses;
using ExecutempsSW.Data.Base.ValidatorClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;

namespace ExecutempsSW.Data.Base.EntityClasses
{
    /// <summary>
    /// Entity class which represents the entity 'Users'.<br/><br/>
    /// 
    /// </summary>
    [Serializable]
    public class UsersEntity : EntityBase2, ISerializable
    {
        #region Class Member Declarations
        private EntityCollection _enablementLogElements;
        private EntityCollection _logoutLogElements;
        private EntityCollection _usersGroupElements;
        private EntityCollection _usersPrivilegeElements;
        private EntityCollection _groupElements;
        private EntityCollection _privilegeElements;
        private ContactEntity _updateContact;
        private ContactEntity _usersContact;
        private UsersKeyEntity _usersKey;



I appreciate your help with this.

Thanks again, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 06-May-2005 10:16:28   

Hmm, it's an IEntity2 object indeed... very strange!

As IEntity2 is located in the ORMSupportClasses, the ONLY reason this fails is that one piece of code has one version of the ORMSupportClasses and the other piece of code another version, perhaps one has the .NET 1.0 version and hte other the 1.1 version, or one has the 1.0.2004.2 version and the other the 1.0.2004.1 version.

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 09-May-2005 23:23:05   

I bought the full product this weekend. Uninstalled the demo copy. Installed the production copy. Created a new project . Generated the code. Re-linked all my existing projects to the newly generated code. Same results.

Any clue?

Thanks, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-May-2005 09:32:45   

Darwin wrote:

I bought the full product this weekend. Uninstalled the demo copy. Installed the production copy. Created a new project . Generated the code. Re-linked all my existing projects to the newly generated code. Same results.

Any clue? Thanks, Darwin

In your own code, you reference the ormsupport classes too. PLease check if you have the .NET 1.1 version referenced there.

Frans Bouma | Lead developer LLBLGen Pro
Darwin avatar
Darwin
User
Posts: 38
Joined: 12-Apr-2005
# Posted on: 10-May-2005 17:32:03   

They all use the .NET11 version. Version #: 1.1.4322

Is that the current version?

Let me know, Darwin

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 10-May-2005 18:01:34   

Darwin wrote:

They all use the .NET11 version. Version #: 1.1.4322

Is that the current version? Let me know, Darwin

No I meant, the ORMSupportClasses library. It should be the SD.LLBLGen.Pro.ORMSupportClasses.NET11.dll and version 1.0.2004.2

Frans Bouma | Lead developer LLBLGen Pro