asp.net-mvcfluent-nhibernatefluent-nhibernate-mappingfluent-migrator

Fluent NHibernate Mapping Doesn't Work For Some Tables


I'm using Fluent Migrator and Fluent NHibernate in the same project but when I launch queries using Database.Session.Query<ENTITY>().** some return data and some don't. I can't tell where I'm doing wrong.

Database Query That Returns Users:

var users = Database.Session.Query<Models.User>()
                .Where(u => u.Id == id)
                .ToList();

Database Query That Doesn't Return Users:

var ourStocks = Database.Session.Query<Models.Stock>()
                .Where(stock => stock.Belongstouser == id)
                .OrderByDescending(stc => stc.StockId)
                .ToList();

Models.User:

public class User
    {
        public virtual int Id { get; set; }
        public virtual string Username { get; set; }
        public virtual string Email { get; set; }
        public virtual string adSoyad { get; set; }
        public virtual string addressMah { get; set; }
        public virtual string addRessCadSk { get; set; }
        public virtual string addressIl { get; set; }
        public virtual string addressIlce { get; set; }
        public virtual int Balance { get; set; }
        public virtual string PasswordHash { get; set; }
        public virtual IList<Roles> Roles { get; set; }

        public User()
        {
            Roles = new List<Roles>();
        }

        public virtual void SetPassword(string password)
        {
            PasswordHash = BCrypt.Net.BCrypt.HashPassword(password, 13);
        }

        public virtual bool CheckPassword(string password)
        {
            var pass = BCrypt.Net.BCrypt.Verify(password, PasswordHash);
            return BCrypt.Net.BCrypt.Verify(password, PasswordHash);
        }      
    }

    public class UsersMap : ClassMapping<User>
    {

        public UsersMap()
        {
            Table("users");
            Schema("projefinalodevi");
            
            Id(x => x.Id, map => map.Generator(Generators.Identity));
            Property(x => x.Username, map => map.NotNullable(true));
            Property(x => x.Email, map => map.NotNullable(true));
            Property(x => x.adSoyad, map => map.NotNullable(true));
            Property(x => x.addressMah, map => map.NotNullable(true));
            Property(x => x.addRessCadSk, map => map.NotNullable(true));
            Property(x => x.addressIl, map => map.NotNullable(true));
            Property(x => x.addressIlce, map => map.NotNullable(true));
            Property(x => x.Balance, map => map.NotNullable(true));
            Property(x => x.PasswordHash, map => { map.Column("password_hash"); map.NotNullable(true); });

            Bag(x => x.Roles, x => {
                x.Table("role_users");
                x.Key(k => k.Column("userid"));
            }, x => x.ManyToMany(k => k.Column("roleid")));

        }
        
    }

Models.Stock:

public class Stock
    {
        public virtual int StockId { get; set; }
        public virtual int Description { get; set; }
        public virtual string Unitprice { get; set; }
        public virtual int Belongstouser { get; set; }
        public virtual int Quantityinstock { get; set; }
        

    }

    public class StockMap : ClassMapping<Stock>
    {
        public StockMap()
        {
            Table("stock");
            Schema("projefinalodevi");
            Lazy(true);
            Id(x => x.StockId, map => { map.Column("stock_id"); map.Generator(Generators.Identity); });
            Property(x => x.Description, map => map.NotNullable(true));
            Property(x => x.Unitprice, map => map.NotNullable(true));
            Property(x => x.Belongstouser, map => map.NotNullable(true));
            Property(x => x.Quantityinstock, map => { map.Column("quantityInStock"); map.NotNullable(true); });
        }
    }

And lastly, my Migration:

public override void Down()
        {
            Execute.Sql("DELETE FROM projeFinalOdevi.roles where name = 'user'");
            Execute.Sql("DELETE FROM projeFinalOdevi.roles where name = 'admin'");
            Delete.Table("role_users");
            Delete.Table("roles");
            Delete.Table("sales");
            Delete.Table("stock");
            Delete.Table("users");
        }

        public override void Up()
        {
            Create.Table("users")
                .WithColumn("id").AsInt32().Identity().PrimaryKey()
                .WithColumn("userName").AsString(50).NotNullable()
                .WithColumn("eMail").AsString(50).NotNullable()
                .WithColumn("balance").AsInt32().NotNullable()
                .WithColumn("adSoyad").AsString(128).NotNullable()
                .WithColumn("addressMah").AsString(40).NotNullable()
                .WithColumn("addressCadSk").AsString(128).NotNullable()
                .WithColumn("addressil").AsString(128).NotNullable()
                .WithColumn("addressilce").AsString(128).NotNullable()
                .WithColumn("password_hash").AsString(256).NotNullable();
            
            Create.Table("stock")
                .WithColumn("stock_id").AsInt32().Identity().PrimaryKey()
                .WithColumn("description").AsString(256).NotNullable()
                .WithColumn("unitPrice").AsInt32().NotNullable()
                .WithColumn("belongsToUser").AsInt32().NotNullable()
                .WithColumn("quantityInStock").AsInt32().NotNullable();

            Create.Table("sales")
                .WithColumn("sales_id").AsInt32().Identity().PrimaryKey()
                .WithColumn("stock_id").AsInt32().ForeignKey("stock", "stock_id").OnDelete(System.Data.Rule.Cascade).NotNullable()
                .WithColumn("date").AsDate().NotNullable()
                .WithColumn("quantity").AsInt32().NotNullable()
                .WithColumn("sale_sum").AsInt32().NotNullable();

            Create.Table("roles")
                .WithColumn("id").AsInt32().Identity().PrimaryKey()
                .WithColumn("name").AsString(128);


            Create.Table("role_users")
                .WithColumn("userid").AsInt32().ForeignKey("users", "id").OnDelete(System.Data.Rule.Cascade)
                .WithColumn("roleid").AsInt32().ForeignKey("roles", "id").OnDelete(System.Data.Rule.Cascade);

            Execute.Sql("INSERT INTO projeFinalOdevi.roles (name) VALUES ('admin')");
            Execute.Sql("INSERT INTO projeFinalOdevi.roles (name) VALUES ('user')");

            /* CMD line;
             migrate -a D:\ProjeFinalOdevi\ProjeFinalOdevi\bin\ProjeFinalOdevi.dll -db MySql -conn "Data Source=127.0.0.1;Database=projefinalodevi;uid=root;pwd=MySQLPassis8420;"
            *//* CMD line;
             migrate -a C:\Users\Atabay\source\repos\ProjeFinalOdevi\ProjeFinalOdevi\bin\ProjeFinalOdevi.dll -db MySql -conn "Data Source=127.0.0.1;Database=ProjeFinalOdevinew;uid=root;pwd=root;"
            */
        }

Help is appreciated, thanks :).


Solution

  • Turns out I was not registering the stock.