This is a migrated thread and some comments may be shown as answers.

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

4 Answers 107 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
TI Scheffer
Top achievements
Rank 2
TI Scheffer asked on 28 Dec 2011, 04:38 PM
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)

4 Answers, 1 is accepted

Sort by
0
TI Scheffer
Top achievements
Rank 2
answered on 29 Dec 2011, 10:51 AM
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.
0
Serge
Telerik team
answered on 30 Dec 2011, 04:37 PM
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!

0
TI Scheffer
Top achievements
Rank 2
answered on 03 Jan 2012, 07:43 PM
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
0
Serge
Telerik team
answered on 06 Jan 2012, 10:23 AM
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!

Tags
Development (API, general questions)
Asked by
TI Scheffer
Top achievements
Rank 2
Answers by
TI Scheffer
Top achievements
Rank 2
Serge
Telerik team
Share this question
or