This project is read-only.

access non-content data from database

Topics: Core, Troubleshooting
Sep 1, 2015 at 4:20 PM
Edited Sep 1, 2015 at 4:25 PM
Hello, everyone!

I have a number of tables in the db that are used to store some data that doesn't belong to the content parts. The problem is whenever I try to query IRepository it is always empty.
I didn't find in the orchard docs on how to handle the non-content data and used some recepies I found online, maybe I missed some conventions or some bindings required for the repositories.

What I did was:

1 I created models like this:
    public class Card
        public virtual int Id { get; set; }

        public virtual string Code { get; set; }

        public virtual DateTime? DateOfActivation { get; set; }

        public virtual bool Confirmed { get; set; }
2 Wrote data migration class:
public class MyDataMigration : DataMigrationImpl
        public int Create()
                table => table
                    .Column<int>("Id", c => c.PrimaryKey().Identity())
                    .Column<string>("Code", c => c.WithLength(50).Unique().NotNull())
                    .Column<DateTime>("DateOfActivation", c => c.Nullable())
                    .Column<bool>("Confirmed", c => c.NotNull())

            return 1;
3 Enabled my module which ran the migration

4 Populated the tables created by the migration using sql scripts, old plain tsql:
    insert into [table_name] ...
5 Created a manager (service) to work with the data:
    public interface ICardManager : IDependency
        Card GetCardByCode(string code);


    public class CardManager : ICardManager
        private IRepository<Card> _cards;

        public CardManager(IRepository<Card> cards)
            this._cards = cards;

        public Card GetCardByCode(string code)
            return _cards.Table.FirstOrDefault(x => x.Code == code);
6 Then I injected the manager into my controller and tried getting a card by its code calling the manager method:
    var card = _cardManager.GetCardByCode(code);
The result is always null, although the code is in the db. The tables are populated and all the data is there. When I tried to list all the cards with "_cards.Table.ToList()" in the manager, the result list was empty.
No exceptions were thrown only empty list was returned.
Sep 1, 2015 at 6:34 PM
Okay. I found out what was the problem. The models weren't in the "<MyModuleName>.Models" namespace. Or at least when I put them there both reading and writing to the db worked well.
Marked as answer by ivanobzherin on 9/1/2015 at 10:35 AM