Custom Id column in the Fluent NH entity name - nhibernate

I am using S#arp architecture with Fluent Nhibernate and Automapper on a legacy DB.
The id column of one of the tables is different from the Automapping convention and therefore I tried to override it without success. I end up with this error
FluentNHibernate.Cfg.FluentConfigurationException
: An invalid or incomplete
configuration was used while creating
a SessionFactory. Check
PotentialReasons collection, and
InnerException for more detail.
Database was not configured
through Database method.
FluentNHibernate.Cfg.FluentConfigurationException
: An invalid or incomplete
configuration was used while creating
a SessionFactory. Check
PotentialReasons collection, and
InnerException for more detail.
Database was not configured
through Database method.
---- NHibernate.MappingException :
(XmlDocument)(3,6): XML validation
error: The element 'class' in
namespace 'urn:nhibernate-mapping-2.2'
has invalid child element 'property'
in namespace
'urn:nhibernate-mapping-2.2'. List of
possible elements expected: 'meta,
subselect, cache, synchronize,
comment, tuplizer, id, composite-id'
in namespace
'urn:nhibernate-mapping-2.2'. ----
System.Xml.Schema.XmlSchemaValidationException
: The element 'class' in namespace
'urn:nhibernate-mapping-2.2' has
invalid child element 'property' in
namespace
'urn:nhibernate-mapping-2.2'. List of
possible elements expected: 'meta,
subselect, cache, synchronize,
comment, tuplizer, id, composite-id'
in namespace
'urn:nhibernate-mapping-2.2'.
How do I user the Id automapper convention and set my custom column as the id through the override functionality?
Note: This is only for one entity. I don’t want to change the general id mapping convention
Here’s my current override function
public class AuthMap : IAutoMappingOverride<Auth>
{
public void Override(AutoMapping<Auth> mapping)
{
mapping.Table("x_auth");
mapping.Map(x => x.Id, "user_id");
mapping.Map(x => x.SessId, "sess_id");
}
}

Figured out:
Use it as
mapping.Id(x => x.Id).Column("user_id");

Related

Configure Swashbuckle (OpenAPI) to Ignore Namespace in Models

On the generated swagger page in the Model section, how does one get Swashbuckle to not report the whole namespace, but just the model?
MyNamespace.SubFolder.MyModel
to
MyModel
You should be able to define custom schema id's either through an ISchemaFilter
or create your own "schema id strategy" read: Customizing Schema Id's
services.AddSwaggerGen(c =>
{
...
c.CustomSchemaIds((type) => type.FullName);
};
You could create a base model class that contains a name property (remember to [JsonIgnore]) use that property for the strategy.

NHibernate (Fluent mapping) ReferencesAny<object>

Base entity interface is IEntity which requires only "object ID {get;set;}" to be implemented.
Now, in almost every case ID is of Guid type (except for membership etc).
I am using following code to do mapping of interface
...
AnyPart<IEntity> primaryMap = ReferencesAny(x => x.Primary)
.IdentityType<object>() // tried with .IdentityType<Guid>()
.EntityIdentifierColumn("PrimaryID")
.EntityTypeColumn("PrimaryType")
.MetaType<string>();
...
Of course, next I am adding meta values.
So, Now getting error
Source array was not long enough. Check srcIndex and length, and the array's lower bound
And with .IdentityType<Guid>()
could not resolve property: Primary.ID of: Founder.Connection [.SingleOrDefault[Founder.Connection](NHibernate.Linq.NhQueryable`1[Founder.Connection], Quote((x, ) => (OrElse(AndAlso(Equal(x.Primary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211), Equal(x.Secondary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211)), AndAlso(Equal(x.Secondary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211), Equal(x.Primary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211))))), )]
UPDATE:
I tried also with .IdentityType(x=>x.ID) but same problem (Source array was not long enough)
UPDATE II:
Query (Actually whole method containing query) that this error occurs on is bellow:
public IQueryable<Connection> GetConnections(IEntity connectable)
{
IQueryable<Connection> query =
Query().Where(
x => x.Primary.ID == connectable.ID || x.Secondary.ID == connectable.ID);
return query;
}
Try this in the query: x.Primary == connectable (without ID).
The problem is that you reference an object (or another unmapped type, that's why you need an any mapping). There is no ID on object.
By the way, using HQL would allow you to access the id by using the keyword id, which is not available in Linq (technically it could be made available as extension method, but I don't know Linq to NH good enough to say if it had been implemented). Every any reference conceptually has an id and a class.

NHibernate mapping by code: Mapping userTypes by convention

I would like to know how to configure NHibernate "mapping by code" so that when I map a property of a certain type, it uses a certain userType to perform that mapping. It figures this out by convention.
For example, if I have an Account class with a property Currency of type Currency, then the NHibernate configuration should figure out (by convention) that it needs to use the CurrencyUserType to perform the mapping.
I'm unable to find the relevant documentation for this, so if such documentation does exist, then a few links will be appreciated too.
Note: This is not a FluentNHibernate question.
var mapper = new ConventionModelMapper();
mapper.IsProperty((info, b) => b || info.GetPropertyOrFieldType() == typeof(Currency));
mapper.BeforeMapProperty +=
(inspector, member, customizer) =>
{
if (member.LocalMember.GetPropertyOrFieldType() == typeof(Currency))
customizer.Type<CurrencyUserType>();
};

Fluent NHibernate: cannot cast PersistentGenericBag to type 'System.Collections.Generic.ISet`

I have an object Topic which is a self-related hierarchy where the child Topics are defined as
public class Topic : Entity {
public ISet<Topic> ChildTopics { get; internal set; }
public Topic ParentTopic { get; set; }
...
}
I'm writing a form (MVC3) to produce a drop-down list (Html.DropDownListFor) of the first-level topics (theoretically this will eventually AJAX into a second drop-down for second-level topics), but when it goes to save, it produces the ever-popular "Cannot cast..." exception (see question title).
The usual cause of this is that you used List or Set instead of IList or ISet, but I am using ISet, and it specifically says it can't cast to ISet.
The reason this is a set is because you wouldn't want a Topic being a child of another Topic more than once. The table mapping create by Fluent NH automapping is correct with this override:
mapping.HasMany<Topic>(t => t.ChildTopics).AsSet().Inverse().KeyColumn("TopicId");
In my project, as of NHibernate 3.2.0.400, this error still occurs if I use System.Collections.Generic.ISet<T> instead of Iesi.Collections.Generic.ISet<T>. Simply changing the references around is minimal work and solves the problem.

Fluent NHibnernate HasManyToMany with Index

I'm trying to map a many-to-many collection with Fluent NHibnernate. My model class has this property:
public virtual IList<Resource> Screenshots
{
get { return _screenshots; }
protected set { _screenshots = value; }
}
And my fluent mapping is:
HasManyToMany(x => x.Screenshots)
.AsList(x => x.WithColumn("Index"))
.Cascade.AllDeleteOrphan();
When I run my application, I get the following exception message:
The element 'list' in namespace
'urn:nhibernate-mapping-2.2' has
invalid child element 'many-to-many'
in namespace
'urn:nhibernate-mapping-2.2'. List of
possible elements expected: 'index,
list-index' in namespace
'urn:nhibernate-mapping-2.2'.
There should be a way to do this. Does anyone know what I am doing wrong?
The current FluentNHibernate syntax for this is as follows:
HasManyToMany(x => x.Screenshots)
.AsList(i => i.Column("`Index`"));
The index column defaults to Index, but that's a reserved word on SQL Server (and probably other databases too), so you must quote it with back-ticks.
Also, I'd recommend against setting a cascade on this relationship. Consider the following code:
x.Screenshots.Remove(s);
session.SaveOrUpdate(x);
NHibernate will correctly delete rows from the linking table even without a cascade specified. However, if you specify AllDeleteOrphan, then NHibernate will delete the row from the linking table and also delete Resource s. I doubt this is the behavior you want on a many-to-many relationship.