nhibernate says 'mapping exception was unhandled' no persister for: MyNH.Domain.User - nhibernate

I am using nHibernate and fluent.
I created a User.cs:
public class User
{
public virtual int Id { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string Email { get; set; }
public virtual DateTime DateCreated { get; set; }
public virtual DateTime DateModified { get; set; }
}
Then in my mappinds folder:
public class UserMapping : ClassMap<User>
{
public UserMapping()
{
WithTable("ay_users");
Not.LazyLoad();
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Username).Not.Nullable().WithLengthOf(256);
Map(x => x.Password).Not.Nullable().WithLengthOf(256);
Map(x => x.Email).Not.Nullable().WithLengthOf(100);
Map(x => x.DateCreated).Not.Nullable();
Map(x => x.DateModified).Not.Nullable();
}
}
Using the repository pattern for the nhibernate blog:
public class UserRepository : Repository<User>
{
}
public class Repository<T> : IRepository<T>
{
public ISession Session
{
get
{
return SessionProvider.GetSession();
}
}
public T GetById(int id)
{
return Session.Get<T>(id);
}
public ICollection<T> FindAll()
{
return Session.CreateCriteria(typeof(T)).List<T>();
}
public void Add(T product)
{
Session.Save(product);
}
public void Remove(T product)
{
Session.Delete(product);
}
}
public interface IRepository<T>
{
T GetById(int id);
ICollection<T> FindAll();
void Add(T entity);
void Remove(T entity);
}
public class SessionProvider
{
private static Configuration configuration;
private static ISessionFactory sessionFactory;
public static Configuration Configuration
{
get
{
if (configuration == null)
{
configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(User).Assembly);
}
return configuration;
}
}
public static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
sessionFactory = Configuration.BuildSessionFactory();
return sessionFactory;
}
}
private SessionProvider()
{ }
public static ISession GetSession()
{
return SessionFactory.OpenSession();
}
}
My config:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=.\SqlExpress;Initial Catalog=TestNH;User Id=dev;Password=123</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
I created a console application to test the output:
static void Main(string[] args)
{
Console.WriteLine("starting...");
UserRepository users = new UserRepository();
User user = users.GetById(1);
Console.WriteLine("user is null: " + (null == user));
if(null != user)
Console.WriteLine("User: " + user.Username);
Console.WriteLine("ending...");
Console.ReadLine();
}
Error:
nhibernate says 'mapping exception was unhandled' no persister for: MyNH.Domain.User
What could be the issue, I did do the mapping?

NH says that you don't have the mapping for the User class.
You probably forgot to include that. Try to call this from your SessionProvider configurator:
Fluently.Configure()
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UserMapping>())
.BuildSessionFactory();

Related

.net Core 3 / API / DI / Repository pattern

I'm trying to create a web API in .net Core. I would like this API calls a DLL with repository design pattern implemented in it. In this whole thing I tried to use dependency injection but I have some issues to manage context for database in repository.
I would like than the context one new context per lifetime of a call to the API.
When I try to execute my code I have an exception at line CreateHostBuilder(args).Build().Run(); in Main.
This Exception is :
'Some services are not able to be constructed'
InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.
I know the code is incomplete and won't work completely but at least it should break way later than actually. I think I made a mistake in the architecture.
There is a lot of code below but I couldn't isolate my problem.
Thank you everybody.
In my API I have :
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IUserService, UserService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
User.cs
public class User
{
public User() {}
public int UserId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime Birthdate { get; set; }
}
UserService.cs
public class UserService : IUserService
{
private readonly AppSettings _appSettings;
private readonly IUsersRepository _userRepository;
public UserService(IOptions<AppSettings> appSettings, IUsersRepository userRepository)
{
_appSettings = appSettings.Value;
_userRepository = userRepository;
}
public IEnumerable<User> GetAll()
{
return _userRepository.GetAllUsers().Select(u=> new User());
}
}
IUserService.cs
public interface IUserService
{
public IEnumerable<User> GetAll();
}
AppSettings.cs
public class AppSettings
{
public string Secret { get; set; }
}
UsersController.cs
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private IUserService _userService { get; }
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpGet]
public IActionResult GetAll()
{
var users = _userService.GetAll();
return Ok(users);
}
}
In the DLL I have :
SecurityContext.cs
public partial class SecurityContext : DbContext
{
public SecurityContext(DbContextOptions<SecurityContext> options) : base(options) { }
public DbSet<Users> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=; Database=BXDB; User Id=sa; Password=;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>(entity =>
{
entity.HasKey(e => e.UserId).HasName("PK_User_UserId");
entity.ToTable("Users", "sec");
entity.Property(e => e.Birthdate).HasColumnType("date");
entity.Property(e => e.FirstName)
.HasMaxLength(50)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(50)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Users.cs
public class Users
{
public Users() { }
public int UserId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime Birthdate { get; set; }
}
IUsersRepository.cs
public interface IUsersRepository
{
public IQueryable<Users> GetAllUsers();
}
UsersRepository.cs
public class UsersRepository : IUsersRepository
{
public readonly SecurityContext _dbContext;
public UsersRepository(SecurityContext dbContext)
{
_dbContext = dbContext;
}
public IQueryable<Users> GetAllUsers()
{
return _dbContext.Users;
}
}
'Some services are not able to be constructed' InvalidOperationException : Unable to resolve service for type 'BX_Security_AccessBase.Context.SecurityContext' while attempting to activate 'BX_Security_AccessBase.Repository.UsersRepository'.
From the error , you should register the DbContext as a service as follows:
public void ConfigureServices(IServiceCollection services)
{
var connection = #"Server=(localdb)\mssqllocaldb;Database=BXDB;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<DLL.Models.SecurityContext>(options => options.UseSqlServer(connection, x => x.MigrationsAssembly("DLL")));
services.AddControllers();
services.AddScoped<IUsersRepository, UsersRepository>();
services.AddScoped<IUserService, UserService>();
}

Nhibernate QueryOver does not work but CreateSqlQuery works

In my project I have a class named User with the following definition:
public class User
{
public virtual Guid UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
}
and here is mapper class:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId).Column("UserId");
Map(x => x.UserName).Column("UserName");
Map(x => x.Password).Column("Password");
}
}
when I try to execute the following code it does not return any result:
public IQueryable<User> GetByUserPassword(string userName, string password)
{
var result = Session.QueryOver<User>()
.Where(x => x.Password == password && x.UserName == userName)
.List<User>();
return result.AsQueryable();
}
But when I use CreateSqlQuery("select * from [dbo].[User]") method it returns value without column names:
and here is my Nhibernate configuration code:
public class NHibernateSessionFactory
{
private ISessionFactory sessionFactory;
private readonly string ConnectionString = "";
public NHibernateSessionFactory(String connectionString)
{
this.ConnectionString = connectionString;
}
public ISessionFactory SessionFactory
{
get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); }
}
public ISessionFactory CreateSessionFactory()
{
try
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(x => x.FromConnectionStringWithKey(ConnectionString)))
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<User>())
.ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
.BuildSessionFactory();
}
catch (Exception ex)
{
throw ex;
}
}
}
what is the problem?
Do not use QueryOver, use just Query:
var result = Session.Query<User>()
.Where(x => x.Password == password && x.UserName == userName);
//already IQueryable<User>
This was helpful but my mistake was a mistake in adding MapClass Assembly which was in other assembly:
.AddFromAssemblyOf()

Adding IAutoMappingOverride with interface for all domain classes

I have an IAuditable interface that defines my class domain as auditable.
public interface IAuditable
{
DateTime CreateAt { get; }
IUser CreateBy { get; }
DateTime? UpdateAt { get; }
IUser UpdateBy { get; }
}
For these classes (there are many) that implement this interface, the configuration is the same! So I decided to override convention:
public class AudityConvention : IAutoMappingOverride<IAuditable>
{
public void Override(AutoMapping<IAuditable> mapping)
{
mapping.Map(p => p.CreateAt).ReadOnly().Access.Property().Not.Nullable().Not.Update().Default("getDate()").Generated.Insert();
mapping.References<Usuario>(p => p.CreateBy).Not.Nullable().Not.Update();
mapping.Map(p => p.UpdateAt).ReadOnly().Access.Property().Default("getDate()").Not.Insert().Generated.Always();
mapping.References<Usuario>(p => p.UpdateBy).Nullable().Not.Insert();
}
}
And configure it
_configuration = Fluently.Configure() // All config from app.config
.Mappings(m =>
{
m.AutoMappings.Add(
AutoMap.AssemblyOf<Usuario>()
.UseOverridesFromAssemblyOf<AudityConvention>()
.Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>())
);
m.FluentMappings
.AddFromAssemblyOf<UsuarioMap>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
;
})
.BuildConfiguration();
SessionFactory = _configuration.BuildSessionFactory();
Session = SessionFactory.OpenSession();
var export = new SchemaExport(_configuration);
export.Drop(false, true); // drop and recreate the database (Just to make sure that the settings are being applied)
export.Execute(false, true, false); // Create de database
With this app.config
<appSettings>
<add key="FluentAssertions.TestFramework" value="mstest"/>
</appSettings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string_name">Data</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Exemple domain class:
public class Entidade : IAuditable
{
public virtual int Id { get; protected set; }
[StringLength(255)]
public virtual string Nome { get; set; }
// Implements IAuditable
public virtual DateTime CreateAt { get; protected set; }
public virtual IUser CreateBy { get; set; }
public virtual DateTime? UpdateAt { get; protected set; }
public virtual IUser UpdateBy { get; set; }
}
And map it:
public class EntidadeMap : ClassMap<Entidade>
{
public EntidadeMap()
{
Id(p => p.Id);
Map(p => p.Nome);
Table("Entidades");
}
}
Results:
Question
What am I doing wrong?
How to create a convention for all classes that implement IAuditable settings are the same!
The part of the configuration below was added later. From what I read, overwrite support only by AutoMappings conventions.
m.AutoMappings.Add(
AutoMap.AssemblyOf<Usuario>()
.UseOverridesFromAssemblyOf<AudityConvention>()
.Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>())
);
I think you're out of luck. I don't believe overrides work on interfaces.
You're better off doing something like this:
public class BaseAuditable : IAuditable
{
....
}
public class Entidade : BaseAuditable
{ ... }
You can use
IAutoMappingOverride<BaseAuditable> instead of IAutoMappingOverride<IAuditable>
I hope I'm wrong but I tried to make it work so many times with interfaces with no luck.
a possiblity
var overrideMethod = typeof(AutoPersistenceModel).GetMethod("Override");
foreach (var type in typeof(IAuditable).Assembly)
{
if (typeof(IAuditable).IsAssignableFrom(type))
{
overrideMethod.MakeGenericMethod(type).Invoke(new Action<IAuditable>(MapAuditables));
}
}

NHibernate - Could not compile the mapping document

It is my firs time using NHibernate, I'm getting source code for a program from my friend after that, the program is running well after that I'm trying to add "Stock.hbm.xml" as following:
`
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NBooks.Core.Models"
assembly="NBooks.Core">
<class name="Stock" table="Stocks" lazy="false">
<id name="ID">
<column name="Stock_ID" />
<generator class="identity" />
</id>
<property name="Stock_name" column="Stock_name" />
<property name="Comp_ID" column="Comp_ID" />
<property name="Stock_Code" column="Stock_Code" />
<property name="Address" column="Address" />
<property name="Nots" column="Nots" />
</class>
</hibernate-mapping>
`
with my class "Stock.cs"
using System;
using System.Collections.Generic;
namespace NBooks.Core.Models
{
public class Stock : BaseModel<Stock>
{
public virtual string Stock_name { get; set; }
public virtual string Stock_Code { get; set; }
public virtual int Comp_ID { get; set; }
public virtual string Notes { get; set; }
public virtual string Address { get; set; }
public virtual bool Inactive { get; set; }
public Stock()
{
}
public Stock(string name)
{
this.Stock_name = name;
}
}
public class StockEventArgs : EventArgs
{
public Stock Stock { get; set; }
public StockEventArgs(Stock Stock)
{
this.Stock = Stock;
}
}
public delegate void StockEventHandler(Stock sender, EventArgs e);
}
the Base model is:
using System;
using System.Collections.Generic;
using NBooks.Core.Util;
using NBooks.Data.NHibernate;
using NHibernate;
namespace NBooks.Core.Models
{
public interface IBaseModel
{
int Id { get; set; }
}
public class BaseModel<T> : IBaseModel
{
IList<string> errors = new List<string>();
public virtual int Id { get; set; }
public virtual bool HasErrors {
get { return errors.Count > 0; }
}
public virtual IList<string> Errors {
get { return errors; }
}
public BaseModel()
{
}
public virtual void Validate()
{
Errors.Clear();
}
public virtual void SaveOrUpdate()
{
ITransaction trans = null;
try {
ISession session = NHibernateHelper.OpenSession();
trans = session.BeginTransaction();
session.SaveOrUpdate(this);
session.Flush();
trans.Commit();
} catch (Exception ex) {
LoggingService.Error(ex.Message);
MessageService.ShowError(ex.Message);
trans.Rollback();
}
}
public virtual void Delete()
{
ITransaction trans = null;
try {
ISession session = NHibernateHelper.OpenSession();
trans = session.BeginTransaction();
session.Delete(this);
session.Flush();
trans.Commit();
} catch (Exception ex) {
LoggingService.Error(ex.Message);
MessageService.ShowError(ex.Message);
trans.Rollback();
}
}
public static T Read(int id)
{
return NHibernateHelper.OpenSession().Load<T>(id);
}
public static IList<T> FindAll()
{
return
NHibernateHelper.OpenSession().CreateCriteria(typeof(T)).List<T>();
}
}
}
when build it appers every thing is well and no errors , when run the error "NHibernate - Could not compile the mapping document Stock.hbm.xml" appears.
Thanx in advance
I noticed you have a typo in you XML:
<property name="Nots" column="Nots" />
I would suggest that you look into using Fluent NHibernate as well. It is strongly typed (for the most part) and the mapping files are easier to read and use lambda expressions so that you don't have to go the XML route.
Your mapping would instead look like this:
public class StockMap(): ClassMap<Stock>
{
public StockMap(){
Id(x => x.Id).Column("Stock_ID").GeneratedBy.Identity();
Map(x => x.Comp_ID);
Map(x => x.Address);
Map(x => x.Notes);
}
}
You have a typo in your mapping
<property name="Nots" column="Nots" />
should be
<property name="Notes" column="Nots" />

fluentnhibernate configuration Error

I have created one console application by watching videos from http://www.d80.co.uk/post/2011/10/06/NHibernate-Video-Tutorial.aspx website. when i tried to executing the code its giving me following error:
An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
but when i have downloaded the code and executed, its working fine. i have checked both the code and are same. so not under stand what i am doing wrong. i am posting my code here please help me to get out of this situation.
NhibernateHelper.cs
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using System;
namespace FluentNHibernate
{
class NhibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory sessionFactory
{
get
{
if (_sessionFactory == null)
{
initialisationFactory();
}
return _sessionFactory;
}
}
private static void initialisationFactory()
{
try
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(#"Server=10.10.10.7;Database=TestDatabase;uid=sa;pwd=9ijnhy6;Trusted_Connection=false;").ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Car>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
}
catch (Exception e)
{
throw;
}
}
public static ISession OpenSession()
{
return sessionFactory.OpenSession();
}
}
}
Program.cs
using System;
using FluentNHibernate.Mapping;
namespace FluentNHibernate
{
internal class Program
{
public static void Main(string[] args)
{
using (var session = NhibernateHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var fordMake = new Make
{
Name = "Ford"
};
var fiestaModel = new Model
{
Name = "Fiesta",
Make = fordMake
};
var car = new Car
{
Make = fordMake,
Model = fiestaModel,
Title = "Dans Car"
};
session.Save(car);
transaction.Commit();
Console.WriteLine("Created Car " + car.Title);
}
}
Console.ReadLine();
}
}
public class MakeMap : ClassMap<Make>
{
public MakeMap()
{
Id(x => x.Id);
Map(x => x.Name);
}
}
public class ModelMap : ClassMap<Model>
{
public ModelMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Make).Cascade.All();
}
}
public class CarMap : ClassMap<Car>
{
public CarMap()
{
Id(x => x.Id).GeneratedBy.HiLo("Car");
Map(x => x.Title);
References(x => x.Make).Cascade.All();
References(x => x.Model).Cascade.All();
}
}
public class Make
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class Model
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Make Make { get; set; }
}
public class Car
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual Make Make { get; set; }
public virtual Model Model { get; set; }
}
}
Your NHibernateHelper.cs is considerably different than mine.
Try changing
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(#"Server=10.10.10.7;Database=TestDatabase;uid=sa;pwd=9ijnhy6;Trusted_Connection=false;").ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Car>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
To:
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")))
.Mappings(cfg =>
{
cfg.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly());
cfg.FluentMappings.Conventions.Add(AutoImport.Never());
})
.ExposeConfiguration(x =>
{
x.SetProperty("current_session_context_class", mode);
})
.BuildSessionFactory();
In Your web.config or app.config add the following within tags:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=10.10.10.7; Initial Catalog=TestDatabase; User Id=sa; Password=9ijnhy6;"/>
</connectionStrings
Hopefully this helps.