Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings, i'm a newbie in Fluent NHibernate, and i wonder, how can i add record to database programmatically, without this:
C#
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:
C#
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:

C#
[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":

C#
[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!
Posted
Updated 19-Aug-14 1:40am
v9

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900