Greetings, i'm a newbie in Fluent NHibernate, and i wonder, how can i add record to database programmatically, without this:
Dictionary d = new Dictionary() { blah, blah };
Users u = new Users() { blah, blah };
d.AddUsers(u);
session.SaveOrUpdate(d);
I want to add record programmatically, in generic mode. I've a DTO entities, that contains all necessary fields, after transferring it back to the server side, i'm injecting values (with OMU Value Injecter) from DTO entity to FH mapped entity...but encountered problems, described above... In other words i want to do something like this:
DtoUsers dtoUsers = dtoUsers;
Users u = new Users()
u.InjectFrom(dtoUsers)
session.SaveOrUpdate(u);
But in this way, i'm losing key reference to the Dictionary Id..
Please note, that there's may be a lot of entities like Users, in the future...
Maybe FH already have a feature, which allows generate a dummy with default values, and already filled necessary fields (like a key references)?
How can i achieve this?
i've got this mapped classes:
[DataContract(IsReference = true)]
public class Dictionary : IAlcoDictionary
{
[DataMember]
public virtual IList<Users> Users { get; protected set; }
[DataMember]
public virtual Int64 ID { get; protected set; }
[DataMember]
public virtual String Name { get; set; }
[DataMember]
public virtual String Description { get; set; }
public Dictionary()
{
Users = new List<Users>();
}
public virtual void AddUsers(Users users)
{
users.Dictionary = this;
Users.Add(users);
}
}
public class DictionaryMap : ClassMap<Dictionary>
{
public DictionaryMap()
{
Id(x => x.ID);
Map(x => x.Name);
Map(x => x.Description);
HasMany<Users>(x => x.Users)
.Inverse()
.Cascade.All();
}
}
And referenced entity called "Users":
[DataContract(IsReference = true)]
public class Users : IAlcoDictionary
{
[DataMember]
public virtual Int64 ID { get; protected set; }
[DataMember]
public virtual String Name { get; set; }
[DataMember]
public virtual String FullName { get; set; }
[DataMember]
public virtual String Password { get; set; }
[DataMember]
public virtual Boolean IsActive { get; set; }
[DataMember]
public virtual DateTime LastLoginDate { get; set; }
[DataMember]
public virtual Dictionary Dictionary { get; set; }
public Users()
{
}
}
public class UsersMap : ClassMap<Users>
{
public UsersMap()
{
Id(x => x.ID);
Map(x => x.Name);
Map(x => x.FullName);
Map(x => x.Password);
Map(x => x.IsActive);
Map(x => x.LastLoginDate);
References(x => x.Dictionary).Not.LazyLoad();
}
}
Thank you!