S开发者_如何学编程o, I need to map my class to database table, but sometimes I need lazy loading to be on, sometimes to be off.
Example: I made duplicates of these two classes described below, and I map them using FNH but with lazy loading on in original, and off in duplicate.
http://i.stack.imgur.com/goG30.png
Basically, I want to be able to get a team from DB that has all lists with TeamMembers fetched but those members should not have all their teams and everything else, just plain info about a TeamMember. Also when i get an TeamMember from DB, I want all its teams to contain only plain info's.
"http://i.stack.imgur.com/7OkyD.png" ->they don't allow new users to post pics or links.
So if I have only those two classes, then if lazy loading is turned on on any side, one of the situations explained before is not satisfied. If lazy is off on both sides I get a whole bunch of data that I don't need nor want.
At first original and duplicate had same names and were in different packages, but I got an exception that mapping was ambiguous. If there is a way for this to work, that would be ideal. Is there a way to do this?
I couldn't find an answer so I changed name of duplicate to be NameOfOriginal+Lite. Mapping was parsed but when I wanted to get a team from database, I get an exception:
{"ORA-00904: \"SUPERVIZ_\".\"TEAMMEMBERLITE_ID\": invalid identifier\n"}
So, apparently FNH reads a class name and ads an "_ID" and uses that as a ID for my duplicate class and that causes the problem. I tried with .ParentKeyColumn("") .ChildKeyColumn("") but no success.
I hope I didn't confuse you too much :)
[DataContract]
public class Team
{
[DataMember]
public virtual int Team_id { get; private set; }
[DataMember]
public virtual String Name { get; set; }
[DataMember]
public virtual String Description { get; set; }
[DataMember]
public virtual TeamMember Deputy { get; set; }
[DataMember]
public virtual TeamMember Leader { get; set; }
[DataMember]
public virtual IList<TeamMember> TeamMembers { get; set; }
[DataMember]
public virtual IList<TeamMember> Supervizors { get; set; }
...
}
[DataContract]
public class TeamMember
{
[DataMember]
public virtual int TeamMember_id { get; set; }
[DataMember]
public virtual String First_name { get; set; }
[DataMember]
public virtual String Last_name { get; set; }
[DataMember]
public virtual String Sid { get; set; }
[DataMember]
public virtual IList<Team> SupevisingTeams { get; set; }
[DataMember]
public virtual IList<Team> LeaderInTeams { get; set; }
[DataMember]
public virtual IList<Team> DeputyInTeams { get; set; }
[DataMember]
public virtual IList<Team> MemberInTeams { get; set; }
...
}
You can map a class multiple times if you give the mapping an entity name. You will need to use ClassMap
for this rather than auto mapping.
Using Fluent NHibernate:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
public class FooMap1 : ClassMap<Foo>
{
public FooMap1()
{
Table("Foo");
EntityName("Foo");
Id(x => x.Id);
Map(x => x.Name);
}
}
public class FooMap2 : ClassMap<Foo>
{
public FooMap2()
{
Table("Foo");
EntityName("Bar");
Id(x => x.Id);
Map(x => x.Name);
}
}
You need to specify the entity name when building the query to make nhibernate use the correct mapping:
using (var session = _sessionFactory.GetCurrentSession())
{
return session.CreateCriteria("Bar")
.Add(Restrictions.Eq("Name", "Andrew"))
.List<Foo>();
}
精彩评论