active record mapping table

时间:2016-02-12 19:56:09

标签: c# asp.net activerecord

I recently started to give support to an application in my job and it uses Castle Active Record and I can´t completely understand it.

What I'm trying to do is to make a query to a new table in the Data Base, I was using a class that was previously used in the application and I created my own class bassed on the other one.

The problem is that the existing class is not very similar to the ones in the documentation, so I have this problem.

My class name is CodigosPostalesModel and I created it like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using Castle.ActiveRecord.Queries;
using System.Collections;

namespace Nutrir.Models
{
[ActiveRecord("CodigosPostales")]
public class CodigosPostalesModel : BaseModel
{
    public const string TableName = "CodigosPostalesModel s";
    public const string ScopeBase = "s.Codigo,s.Asentamiento,s.Municipio,s.Estado";        
    public const string ScopeFilterById = "s.Codigo = '54070'";

    [PrimaryKey(PrimaryKeyType.Native)]
    private int IdCodigo { get; set; }

    [Property]
    public string Codigo { get; set; }

    [Property]
    public string Asentamiento { get; set; }

    [Property]
    public string d_tipo_asenta { get; set; }
    [Property]
    public string Municipio { get; set; }

    [Property]
    public string Estado { get; set; }

    [Property]
    public string d_CP { get; set; }

    [Property]
    public string c_estado { get; set; }
    [Property]
    public string c_oficina { get; set; }

    [Property]
    public string c_CP { get; set; }

    [Property]
    public string c_tipo_asenta { get; set; }

    [Property]
    public string c_mnpio { get; set; }

    [Property]
    public string id_asenta_cpcons { get; set; }

    [Property]
    public string d_zona { get; set; }

    [Property]
    public string c_cve_ciudad { get; set; }

    public static void SetFrom(HqlBasedQuery query)
    {
        query.Query += String.Format(" {0} {1}", SqlFrom, TableName);
    }

    public static HqlBasedQuery GetBase()
    {
        string query = string.Format("{0} {1}", SqlSelect, ScopeBase);
        HqlBasedQuery q = GetQuery(query);
        SetFrom(q);
        return q;
    }

    public static CodigosPostalesModel FindByCodigo(string codigo)
    {
        CodigosPostalesModel el = null;
        object[] result;
        HqlBasedQuery query = GetBase();
        AddWhere(query);
        SetFilterById(query, codigo);
        result = GetFirstElement(query);
        if (result != null)
        {
            el = GetElement(result);
        }
        return el;
    }
    public static void SetFilterById(HqlBasedQuery query, string codigo)
    {
        query.Query += string.Format(" {0}", ScopeFilterById);
        query.SetParameter("Codigo", codigo);
    }
    public static CodigosPostalesModel GetElement(object[] data)
    {
        CodigosPostalesModel el = new CodigosPostalesModel();
        el.Codigo = GetObjectString(0, data);
        el.Asentamiento = GetObjectString(0, data);
        el.Municipio = GetObjectString(0, data);
        el.Estado = GetObjectString(0, data);
        return el;
    }

}
}

Once the methods ran I get a query like this

SELECT s.Codigo,sAsentamiento,s.Municipio,s.Estado FROM CodigosPostalesModel s WHERE s.Codigo= '54090'

After all I get the error

CodigosPostalesModel is not mapped [SELECT s.Codigo,s.Asentamiento,s.Municipio,s.Estado FROM CodigosPostalesModel s WHERE s.Codigo = '54090']

I don't know what is wrong in my Mapping... Help Please!!

0 个答案:

没有答案