How i have to map fields who aren't primitive objects?

5 posts, 0 answers
  1. TI Scheffer
    TI Scheffer avatar
    4 posts
    Member since:
    Jul 2011

    Posted 28 Dec 2011 Link to this post

    I'm having trouble to map my table.

    The following error message is showing, every time.
    The metadata for field 'lado' of class 'NewCastle.Modelo.Celulas.Celula' cannot be initialized: No database type mapping found for field 'lado': CLR type 'NewCastle.Modelo.Lado' or column type '' unmapped.

    Table (attached).

    Class Celula:
    public sealed class Celula
    {
      internal Celula() { }

      public string pk_Celula { get; private set; }
      public short Corredor { get; set; }
      public short Ala { get; set; }
      public short Andar { get; set; }
      public Lado Lado { get; set; }
      public Profundidade Profundidade { get; set; }
      public Situacao Situacao { get; set; }
    }

    Struct Lado:
    public struct Lado
    {
      public static readonly IDictionary<int, Lado> Lista = new Dictionary<int, Lado>();
      public static readonly Lado Direito = new Lado(20, "Direito", "D");
      public static readonly Lado Esquerdo = new Lado(10, "Esquerdo", "E");

      
    private
    readonly int _Valor;
      private readonly string _Nome;
      private readonly string _nomeCurto;

      
    public
    Lado(int valor, string nome, string nomeCurto)
      {
        this._Valor = valor;
        this._Nome = nome;
        this._nomeCurto = nomeCurto;
        Lista.Add(
    this._Valor, this);
      }

      static public implicit operator char(Lado lado)...
      static public implicit operator Lado(char lado)...
      
      public string nomeCurto()
      {
        return this._nomeCurto;
      }
      
      public override string ToString()
      {
        return this._Nome;
      }
    }

    Mapping (only Celula's block):

    MappingConfiguration<Celula> celulaConfiguration = new MappingConfiguration<Celula>();
      celulaConfiguration.MapType(p => new
        {
          pk_Celula = p.pk_Celula,
          Corredor = p.Corredor,
          Ala = p.Ala,
          Andar = p.Andar,
          Lado = p.Lado,
          Profundidade = p.Profundidade,
          Situacao = p.Situacao
        }).ToTable(this.obterNome("Celulas"));
      celulaConfiguration.HasProperty(p => p.pk_Celula).IsIdentity();
      celulaConfiguration.HasArtificialPrimitiveProperty<char>("Lado");
      configurations.Add(celulaConfiguration);

    The question is, how i have to map fields who aren't primitive objects (and who are prepared to get primitive values, and automatically convert to field type) in domain and in database they are?
    Database = char
    Domain = Lado (struct)
  2. TI Scheffer
    TI Scheffer avatar
    4 posts
    Member since:
    Jul 2011

    Posted 29 Dec 2011 Link to this post

    Lado, Profundidade and Situacao who are Struct s, are having problem.
    I have temporary fixed this, using this:

    public Lado Lado
    {
      get { return lado; }
      set { lado = value; }
    }
    public char lado { get; set; }

    Lado is the name of my old property, who i'm using in my domain.
    lado is the name of my property, who are receiving the data of ORM.
  3. DevCraft banner
  4. Serge
    Admin
    Serge avatar
    375 posts

    Posted 30 Dec 2011 Link to this post

    Hi,

     Unfortunately OpenAccess does not support models that contain structs and complex types at the moment. In order to achieve something similar you will need to create classes instead of structs and threat them as associations, however I would advice against that. 

    Please give us a bit more insight on what you are trying to achieve, and why you feel a struct is needed there. It might be that there is a better solution to your situation. 

    Kind regards,
    Serge
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

  5. TI Scheffer
    TI Scheffer avatar
    4 posts
    Member since:
    Jul 2011

    Posted 03 Jan 2012 Link to this post

    Hi,
    Telerik OpenAccess already works (support) with this functionallity (Telerik Team should know this already, the usability for WithConverter method), I've do a little code adaptation, who have work correctly.

    Class LadoTypeConverter -> convert the type, from lado to char and contrariwise.
    public class LadoTypeConverter : AdoTypeConverter
    {
      private bool nullable;
     
      public override bool CreateLiteralSql(ref DataHolder holder)
      {
        holder.StringValue = (holder.NoValue) ? "NULL" : holder.CharValue.ToString();
        return (!holder.NoValue);
      }
     
      public override Type DefaultType
      {
        get { return typeof(char); }
      }
     
      public override object Read(ref DataHolder holder)
      {
        holder.NoValue = holder.Reader.IsDBNull(holder.Position);
        if (holder.NoValue)
        {
          if (nullable)
            holder.ObjectValue = null;
          else
            if (!holder.Box)
              holder.CharValue = Lado.Direito;
            else
              holder.ObjectValue = Lado.Direito;
        }
        else
        {
          Lado parsedCharValue = Char.Parse(holder.Reader.GetValue(holder.Position).ToString());
          if ((!nullable) && (!holder.Box))
            holder.CharValue = parsedCharValue;
          else
            holder.ObjectValue = parsedCharValue;
        }
        return holder.ObjectValue;
      }
     
      public override void Write(ref DataHolder holder)
      {
        holder.Parameter.DbType = System.Data.DbType.StringFixedLength;
        string valor = (holder.NoValue) ? null : Convert.ToString(((char)(Lado)holder.ObjectValue));
        holder.Parameter.Value = valor;
        holder.Parameter.Size = valor.Length;
      }
     
      public override AdoTypeConverter Initialize(IDataColumn user, System.Type clr, Telerik.OpenAccess.Data.IAdoTypeConverterRegistry registry, bool secondaryTable)
      {
        if (clr == typeof(Lado) && user.ResolvedSqlType == "char")
        {
          nullable = typeof(Lado?) == clr;
          return base.Initialize(user, clr, registry, secondaryTable);
        }
     
        return null;
      }
    }

    Class Mapeamento -> Mapping
    MappingConfiguration<Celula> celulaConfiguration = new MappingConfiguration<Celula>();
          celulaConfiguration.MapType(p => new
            {
              pk_Celula = p.pk_Celula,
              Corredor = p.Corredor,
              Ala = p.Ala,
              Andar = p.Andar
            }).ToTable(this.obterNome("Celulas"));
          celulaConfiguration.HasProperty(p => p.pk_Celula).IsIdentity();
          celulaConfiguration.HasProperty(p => p.Lado).ToColumn("Lado").HasColumnType("char").WithConverter<LadoTypeConverter>().IsNotNullable();
          celulaConfiguration.HasProperty(p => p.Profundidade).ToColumn("Profundidade").HasColumnType("smallint").WithConverter<ProfundidadeTypeConverter>().IsNotNullable();
          celulaConfiguration.HasProperty(p => p.Situacao).ToColumn("Situacao").HasColumnType("smallint").WithConverter<SituacaoTypeConverter>().IsNotNullable();
          configurations.Add(celulaConfiguration);

    Thanks,
    Raphael Augusto Rogenski
  6. Serge
    Admin
    Serge avatar
    375 posts

    Posted 06 Jan 2012 Link to this post

    Hi Raphael,

     I would like to apologize for not coming up with this solution, it is in fact a good one and it is great that is shared with the community trough this forum thread. 

    Kind regards,
    Serge
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

Back to Top
DevCraft banner