I have a table in the database that is used only for holding some ID's descriptions:
TABLE SomeClass
- Columns
- ClassTypeID INT CONSTRAINT etc
TABLE SomeClassTypes
- ClassTypeID INT IDENTITY
- Description NVARCHAR
It's done like this so it's easy for users to insert/remove new types. I want to get a report of all of SomeClass
, but I'd like to have a string property to hold the description from the other table:
public class SomeClass
{
public virtual int SomeClassID { get; set; }
public virtual int ClassTypeID { get; set; }
public virtual string DescriptionType { get; set; }
}
public class SomeClassMap : ClassMapping<SomeClass>
{
public SomeClassMap()
{
Table("SomeClassTable");
Property(p => p.SomeClassID, map =>
{
map.Column("SomeClassID");
map.Generator(Generators.Identity);
});
Property(p => p.ClassTypeID, map => map.Column("ClassTypeID"));
//Other properties here
Property(p => p.DescriptionType, ?); //This line
}
}
How can I do this?
Try to use joined table.
public class SomeClassMap : ClassMapping<SomeClass>
{
public SomeClassMap()
{
Table("SomeClassTable");
Property(p => p.SomeClassID, map =>
{
map.Column("SomeClassID");
map.Generator(Generators.Identity);
});
Property(p => p.ClassTypeID, map => map.Column("ClassTypeID"));
//Other properties here
Join("SomeClassTypes", m =>
{
m.KeyColumn("ClassTypeId");
m.Fetch.Join();
m.Map(x => x.DescriptionType).Column("Description");
})
}
}
EDITED
If you use fluent mapping built in NHibernate, try this:
public class SomeClassMap : ClassMapping<SomeClass>
{
public SomeClassMap()
{
Table("SomeClassTable");
Property(p => p.SomeClassID, map =>
{
map.Column("SomeClassID");
map.Generator(Generators.Identity);
});
Property(p => p.ClassTypeID, map => map.Column("ClassTypeID"));
//Other properties here
Property(p => p.DescriptionType, ?); //This line
Join("SomeClassTypes", m =>
{
m.Key(k => k.Column("ClassTypeId"));
m.Fetch(FetchKind.Join);
m.Property(x => x.DescriptionType).Column("Description");
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.