Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:28:28+00:00 2026-05-14T06:28:28+00:00

I’m updating a program and adding a new table to the database. The program

  • 0

I’m updating a program and adding a new table to the database. The program uses Castle’s ActiveRecord with repositories. I’ve set the classes and repository up and can get the test case out of the database fine. However, when I try to add a new record to the database, nothing happens. No error and no new record. If I get the test case out of the database and change something then call Save(), the record in the database changes.

This is the code I’m using to test:

ICountryRepository _countryRepository = Country.GetRepository(site.Id);

//get test case and update
Country c = _countryRepository.FindById(1).Value;
c.Name = "c";
_countryRepository.Save(c);

//create new Country and save
Country d = new Country();
d.Id = 2;
d.Name = "d";
_countryRepository.Save(d);

Now as it’s a maintenence project with no real time to stop and study how the Castle framework is doing everything, I’m learning as I go along. I’ve picked up how to do things by studying the rest of the code and there are occasions in the code where the above code has been used to create new records, so I’m sure Save() is the right function to call.

I’ve tried dropping the create code in with another part of the code that inserts an object into a different table, just to make sure there weren’t different levels of permission in play. There are no differences in the database between the table I added and the table I’m basing my code off.

As I’ve said, my experiences with ActiveRecord and Castle’s framework here are few, it could/will be something quite simple. So hopefully someone can point it out to me.

Thanks.

Edit:

Country Class:

[ActiveRecord("Country")]
[SearchEntity]
public class Country : AbstractEntity<Country, ICountryRepository>
{
    int _countryId;
    string _name;
    int _action;

    [PrimaryKey("CountryId")]
    public int Id
    {
        get { return _countryId; }
        set { _countryId = value; }
    }

    [SearchProperty]
    [Property(Length = 100)]
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    [Property]
    public int PostRegisterActionId
    {
        get { return _action; }
        set { _action = value; }
    }
}

AbstractRepository since CountryRepository does nothing at the moment:

[Transient]
public abstract class AbstractRepository<T> : IRepository<T> where T : AbstractEntity, new()
{
    #region Private Vars
    protected FutureQueryRunner _futureQueryRunner;
    protected IDynamicSearchService _dynamicSearchService;
    protected bool _caching;
    protected int _cachedPages;
    protected CachingFutureQueryOfList<T> _cachedQuery;
    protected IServiceBus _serviceBus;
    private string _entityTypeName = string.Empty;
    #endregion

    #region Constructors
    public AbstractRepository(IDynamicSearchService dynamicSearchService, IServiceBus serviceBus)
    {
        _dynamicSearchService = dynamicSearchService;
        _serviceBus = serviceBus;
    }
    #endregion

    #region Public Methods
    public virtual void Save(T instance)
    {
        ActiveRecordMediator<T>.Save(instance);
    }

    public virtual void Create(T instance)
    {
        ActiveRecordMediator<T>.Create(instance);
    }

    public void Delete(T instance)
    {
        ActiveRecordMediator<T>.Delete(instance);
    }

    public virtual IFutureQueryOf<T> New()
    {
        return new NullFutureQuery<T>();
    }

    public virtual IFutureQueryOf<T> FindById(int id/*, params string[] eagerAssociations*/) // eager associations buggy
    {
        DetachedCriteria criteria = GetDefaultCriteria()
                .Add(Expression.IdEq(id));

        /*foreach (string eager in eagerAssociations)
            criteria.SetFetchMode(eager, NHibernate.FetchMode.Eager);*/

        return new FutureQueryOf<T>(_futureQueryRunner)
            .SetCriteria(criteria);
    }

    public virtual IFutureQueryOfList<T> FindAll(string sortBy, bool sortAsc)
    {
        DetachedCriteria criteria = GetDefaultCriteria();

        if (!string.IsNullOrEmpty(sortBy))
            criteria.AddOrder(sortAsc ? NHibernate.Criterion.Order.Asc(sortBy) : NHibernate.Criterion.Order.Desc(sortBy));

        return new FutureQueryOfList<T>(_futureQueryRunner)
            .SetCriteria(criteria);
    }

    public virtual IFutureQueryOfList<T> FindAll(int page, int resultsPerPage, string sortBy, bool sortAsc)
    {
        return FindAll(new DefaultCriteriaProvider<T>(DetachedCriteria.For<T>()),
            page,
            resultsPerPage,
            sortBy,
            sortAsc,
            "FindAll");
    }

    public virtual IFutureQueryOfList<T> FindAll(string searchString, int page, int resultsPerPage, string sortBy, bool sortAsc)
    {
        return FindAll(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString),
            page,
            resultsPerPage,
            sortBy,
            sortAsc,
            "FindAllSearchString_" + searchString);
    }

    public virtual IFutureQueryOfList<T> FindAll(IListFilter filter, int page, int resultsPerPage, string sortBy, bool sortAsc)
    {
        return FindAll(new FilterCriteriaProvider<T>(_dynamicSearchService, filter),
            page,
            resultsPerPage,
            sortBy,
            sortAsc,
            "FindAllListFilter"); // TODO - the cache key needs to represent individual filters
    }

    public virtual IFutureQueryOf<int> GetCount(string searchString)
    {
        return new FutureQueryOf<int>(_futureQueryRunner)
            .SetCriteria(AddDefaultCriteria(new SearchStringCriteriaProvider<T>(_dynamicSearchService, searchString).GetDetachedCriteria())
                .SetProjection(Projections.RowCount()));
    }

    public virtual IFutureQueryOf<int> GetCount()
    {
        return new FutureQueryOf<int>(_futureQueryRunner)
            .SetCriteria(GetDefaultCriteria()
                .SetProjection(Projections.RowCount()));
    }

    public virtual string EntityType
    {
        get
        {
            if (string.IsNullOrEmpty(_entityTypeName))
                _entityTypeName = typeof(T).Name;
            return _entityTypeName;
        }
    }

    public IRepository<T> EnableCaching(bool caching)
    {
        _caching = caching;
        return this;
    }

    public IRepository<T> WithPagesToCache(int cachedPages)
    {
        _cachedPages = cachedPages;
        return this;
    }

    public virtual IRepository<T> ForSite(int siteId)
    {
        return this;
    }

    public IRepository<T> RunFutureQueriesWith(FutureQueryRunner futureQueryRunner)
    {
        _futureQueryRunner = futureQueryRunner;
        return this;
    }
    #endregion

    #region Protected Methods
    protected virtual DetachedCriteria AddDefaultCriteria(DetachedCriteria criteria)
    {
        return criteria;
    }
    protected DetachedCriteria GetDefaultCriteria()
    {
        return AddDefaultCriteria(DetachedCriteria.For<T>());
    }
    protected IFutureQueryOf<U> NewQueryOf<U>(DetachedCriteria criteria)
    {
        return new FutureQueryOf<U>(_futureQueryRunner).SetCriteria(criteria);
    }
    protected IFutureQueryOfList<U> NewQueryOfList<U>(DetachedCriteria criteria)
    {
        return new FutureQueryOfList<U>(_futureQueryRunner).SetCriteria(criteria);
    }
    #endregion

    #region Private Methods
    private IFutureQueryOfList<T> FindAll(ICriteriaProvider<T> criteriaProvider, int page, int resultsPerPage, string sortBy, bool sortAsc, string cacheKey)
    {
        CachingFutureQueryOfList<T> rtnVal = null;
        bool cached = false;
        if (_cachedQuery != null && _caching)
        {
            rtnVal = _cachedQuery;
            cached = rtnVal.SetPage(page, sortBy, sortAsc, cacheKey);
        }
        if (!cached)
        {
            rtnVal = new CachingFutureQueryOfList<T>(_futureQueryRunner, page, _cachedPages, resultsPerPage, cacheKey)
                .SetCriteria(AddDefaultCriteria(criteriaProvider.GetDetachedCriteria()), sortBy, sortAsc);

            if (_caching)
                _cachedQuery = rtnVal;
        }
        return rtnVal;
    }
    #endregion

    #region Criteria Providers
    private interface ICriteriaProvider<U>
    {
        DetachedCriteria GetDetachedCriteria();
    }

    private class DefaultCriteriaProvider<U> : ICriteriaProvider<U>
    {
        private DetachedCriteria _criteria;
        public DefaultCriteriaProvider(DetachedCriteria criteria)
        {
            _criteria = criteria;
        }
        public DetachedCriteria GetDetachedCriteria()
        {
            return _criteria;
        }
    }

    private class SearchStringCriteriaProvider<U> : ICriteriaProvider<U>
    {
        private IDynamicSearchService _searchService;
        private string _searchString;
        public SearchStringCriteriaProvider(IDynamicSearchService searchService, string searchString)
        {
            _searchService = searchService;
            _searchString = searchString;
        }
        public DetachedCriteria GetDetachedCriteria()
        {
            return _searchService.GetDetachedCriteria<U>(_searchString);
        }
    }

    private class FilterCriteriaProvider<U> : ICriteriaProvider<U>
    {
        private IDynamicSearchService _searchService;
        private IListFilter _filter;
        public FilterCriteriaProvider(IDynamicSearchService searchService, IListFilter filter)
        {
            _searchService = searchService;
            _filter = filter;
        }
        public DetachedCriteria GetDetachedCriteria()
        {
            return _searchService.GetDetachedCriteria<U>(_filter);
        }
    }
    #endregion
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:28:29+00:00Added an answer on May 14, 2026 at 6:28 am

    If you’re explicitly setting the Country PK, as it seems you’re doing, you probably need to call Create() instead of Save() (I don’t know if your repository implementation exposes this).

    If this didn’t work, please post your class mappings and repository implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Quick & dirty: string[] data = null; using (StreamReader sr… May 14, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer It is pretty easy actually, however it relies on an… May 14, 2026 at 7:51 pm
  • Editorial Team
    Editorial Team added an answer You could use reflection. ArrayList<Integer> list = new ArrayList<Integer>(); Field[]… May 14, 2026 at 7:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.