Multiple Field Identity Problem

3 posts, 0 answers
  1. Dave
    Dave avatar
    7 posts
    Member since:
    Dec 2008

    Posted 18 Jan 2012 Link to this post

    Hi there!

    I have a table with only two uniqueidentifier-colums. It's the UserInRoles-table.

    My mapping is the following:
    private MappingConfiguration<ProChristFluentUserInRole> MapProChristUserInRoleTable()
            {
                var tableMapping = new MappingConfiguration<ProChristFluentUserInRole>();
                tableMapping.MapType().ToTable("ProChristUsersInRoles");
     
                tableMapping.HasIdentity(KeyGenerator.Verify);
     
                tableMapping.HasProperty(t => t.UserId).HasColumnType("uniqueidentifier").IsNotNullable();
                tableMapping.HasProperty(t => t.RoleId).HasColumnType("uniqueidentifier").IsNotNullable();
     
                return tableMapping;
            }

    The definition of the class and the id:
    public class ProChristFluentUserInRoleIdentity : IObjectId
        {
            public Guid UserId { get; set; }
     
            public Guid RoleId { get; set; }
     
            public ProChristFluentUserInRoleIdentity()
            {
                UserId = Guid.NewGuid();
                RoleId = Guid.NewGuid();
            }
     
            public ProChristFluentUserInRoleIdentity(string s)
            {
                UserId = Guid.NewGuid();
                RoleId = Guid.NewGuid();
     
                var parts = s.Split('*');
     
                Guid tUserId;
                if (Guid.TryParse(parts[0], out tUserId))
                {
                    UserId = tUserId;   
                }
     
                Guid tRoleId;
                if (Guid.TryParse(parts[1], out tRoleId))
                {
                    RoleId = tRoleId;
                }
            }
     
            public override bool Equals(object o)
            {
                if (this == o) return true;
                if (!(o is ProChristFluentUserInRoleIdentity)) return false;
                var id = (ProChristFluentUserInRoleIdentity)o;
                if (UserId != id.UserId) return false;
                if (RoleId != id.RoleId) return false;
                return true;
            }
     
            public override int GetHashCode()
            {
                var result = 0;
                if (UserId != Guid.Empty)
                    result += UserId.GetHashCode();
                if (RoleId != Guid.Empty)
                    result += RoleId.GetHashCode();
                return result;
            }
     
            public override string ToString()
            {
                var buffer = new System.Text.StringBuilder();
                buffer.Append(UserId).Append('*');
                buffer.Append(RoleId);
                return buffer.ToString();
            }
        }
     
        [Persistent(Identity = typeof(ProChristFluentUserInRoleIdentity))]
        public class ProChristFluentUserInRole
        {
            /// <summary>
            /// Gets or sets the user-Id.
            /// </summary>
            /// <value>
            /// The user-Id.
            /// </value>
            public Guid UserId { get; set; }
     
            /// <summary>
            /// Gets or sets the role-Id.
            /// </summary>
            /// <value>
            /// The role-Id.
            /// </value>
            public Guid RoleId { get; set; }
        }

    But, when I try to make a request, I get the following error:
    [DataStoreException: Count(*) query failed: Telerik.OpenAccess.RT.sql.SQLException: Invalid column name 'ProChristUsersInRoles_id'.
    Statement(s) could not be prepared.
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()
       at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery()
       at OpenAccessRuntime.Relational.RelationalStorageManager.executeCount(RelationalCompiledQuery cq, QueryParameters parameters)
    SELECT COUNT(1) FROM ( SELECT a.[ProChristUsersInRoles_id] AS COL1, a.[RoleId] AS COL2, a.[UserId] AS COL3 FROM [ProChristUsersInRoles] a WHERE a.[UserId] = ?                       AND a.[RoleId] = ?                        ) AS TMP_COUNT

    That is right, because there's no column with name: ProChristUsersInRoles_id
    I've tried different KeyGenerator-Types, but without any luck.

    I don't know how to solve this problem. Can you help me, please?

    Thanks in advance, Dieter
  2. Dave
    Dave avatar
    7 posts
    Member since:
    Dec 2008

    Posted 18 Jan 2012 Link to this post

    Ok, I've changed the class to:
    [Persistent(Identity = typeof(ID))]
       public class ProChristFluentUserInRole
       {
           /// <summary>
           /// Gets or sets the user-Id.
           /// </summary>
           /// <value>
           /// The user-Id.
           /// </value>
           public Guid UserId { get; set; }
     
           /// <summary>
           /// Gets or sets the role-Id.
           /// </summary>
           /// <value>
           /// The role-Id.
           /// </value>
           public Guid RoleId { get; set; }
     
           public class ID : IObjectId
           {
               public Guid UserId { get; set; }
     
               public Guid RoleId { get; set; }
     
               public ID()
               {
                   UserId = Guid.NewGuid();
                   RoleId = Guid.NewGuid();
               }
     
               public ID(string s)
               {
                   UserId = Guid.NewGuid();
                   RoleId = Guid.NewGuid();
     
                   var parts = s.Split('*');
     
                   Guid tUserId;
                   if (Guid.TryParse(parts[0], out tUserId))
                   {
                       UserId = tUserId;
                   }
     
                   Guid tRoleId;
                   if (Guid.TryParse(parts[1], out tRoleId))
                   {
                       RoleId = tRoleId;
                   }
               }
     
               public override bool Equals(object o)
               {
                   if (this == o) return true;
                   if (!(o is ID)) return false;
                   var id = (ID)o;
                   if (UserId != id.UserId) return false;
                   if (RoleId != id.RoleId) return false;
                   return true;
               }
     
               public override int GetHashCode()
               {
                   var result = 0;
                   if (UserId != Guid.Empty)
                       result += UserId.GetHashCode();
                   if (RoleId != Guid.Empty)
                       result += RoleId.GetHashCode();
                   return result;
               }
     
               public override string ToString()
               {
                   var buffer = new System.Text.StringBuilder();
                   buffer.Append(UserId).Append('*');
                   buffer.Append(RoleId);
                   return buffer.ToString();
               }
           }
       }
    but still the same problem
  3. DevCraft banner
  4. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 20 Jan 2012 Link to this post

    Hi Dieter,

    you need to mark the fields that should form the identity:

    var mapping = new MappingConfiguration<Simple>();

    mapping.MapType();

    mapping.HasProperty(x => x.Id).IsIdentity();


    Here, the HasProperty() marks the property as persistent, and then tells the configuration that this field is an identity field.

    All the best,
    Thomas
    the Telerik team

    SP1 for Q3’11 of Telerik OpenAccess ORM is available for download

Back to Top