Mapping items-itemtags-tags tables with NHibernate - nhibernate

Let's say I have this database design:
Items
Id
Name
Desc
ItemTags
ItemId
TagId
Tags
Id
Tag
And I want to map it to the following class
class Item
int Id
string Name
string Desc
IList<string> Tags
Please note that I don't want to declare a Tag class, I just want the Item class to have a list of strings that represent tags.
Is this possible?

Why dont you want a Tag class?it will be strongly typed and alot easier to implement...
Ref:
IList< Tag> Tags;
override the ToString() to write the "Tag" property if that makes it better..

Related

Nhibernate query-over syntax how to filter for a given alias for a subclass type

I have few classes mapped as subclasses to a table using a discriminator column. But the discriminator column is not mapped to any field (even in the base class). I want to create Query (in QueryOver syntax) which filters by the type of the subclass
E.g. Table:
Orders (Id,OrderType,Amount,Qty...)
// OrderType is the discriminator column
mapped classes:
DeliveryOrder (Id,Amount,Qty...)
WorkOrder (Id,Qty,...)
SalesOrder (Id,Amount,...)
Need a query something like
Query.Where(()=>_orderAlias is DeliveryOrder)
I remember this really cool answer by Andrew Whitaker to the question:
How can QueryOver be used to filter for a specific class?
and it shows syntax like this:
q = q.Where(b => b.GetType() == typeof(DeliveryOrder));

enum as string and number in the database

i have a table
table Foo
(
id,
FooType,
FooTypeName,
)
FooType is an enum with fixed values hence i want to map it to
public virtual FooType FooType { get; set; }
How can i serialize the property in both columns but use only one to read and query?
Which mapping (xml, Mapping by Code, FluentMapping) doesn't matter
You could create FooTypeName as a string property projecting the value of FooType, and map it as readonly.
Alternatively, if you don't need FooTypeName at all, just ignore it in your model and keep it updated with a trigger.
Or, if you feel like coding a lot, create a user type that maps FooType to the two columns.

NHibernate Criteria, select by count of property

Sorry for the cryptic title..
Could you help me out, on how do a select, based on the count of a property, using Criteria?
I have an object (Pool) with a property (PoolItems), and i want to select all the Pools w. more than 5 PoolItems.
Try this:
DetachedCriteria dCriteria = DetachedCriteria.For<PoolItem>("pItem")
.SetProjection(Projections.Count("Id"))
.Add(Restrictions.EqProperty("pItem.PoolID", "pool.Id"));
IList<Post> posts = Session.CreateCriteria<Pool>("pool")
.Add(Subqueries.Gt(5, dCriteria)).List<Pool>();
Assuming the PoolItem table has a PoolID column as a foreign key of the Pool table.
The relationship is a one-to-many. If you do not have the PoolID property mapped in the PoolItem class and you just have the many-to-one object mapping named "Pool" then replace in the detached criteria the "pItem.PoolID" with "pItem.Pool.Id".

What's the best way to handle "type" tables with LINQ to SQL?

I have some tables that represent various types. They usually just consist of an ID (int), and a name. Ideally I would have this in an enum. Is there a way to map a table like this onto an enum?
EDIT: How would I handle it if there were extra fields other than an ID and Name?
If it's just an id and a name, I generally do this:
public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}
and then on the LINQ entity property:
[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }
For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.

NHibernate Mapping from multiple tables

Consider the following table structure...
Appointment
-----------
ID integer
Description nvarchar
StatusID smallint
Status
------
ID smallint
DisplayText nvarchar
Now, for good or for evil, we want this situation to map to a class that looks like this
class Appointment
{
public int ID {g;s;}
public string Description { g;s; }
public string Status { g; private s; }
}
I.e. we want to pull the normalised status display text straight into the entity.
The obvious answer is to create a Status entity and make the appointment class have a reference to that and map it in the normal way.
Don't create an entity class. Use an enum and EnumStringType as shown here. This is exactly what you want I think.