4 Replies - 2450 Views - Last Post: 14 July 2011 - 04:29 AM Rate Topic: -----

#1 nighttrain  Icon User is online

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 333
  • Joined: 22-September 10

Fluent NHibernate question

Posted 13 July 2011 - 03:31 AM

Hi there !

I got question about Fluent NHibernate. I have a MySQL database, i create Classes of my Entites and Classes of their Maps.
I got 3 Entities:
1. Client (client_id, name, surname)
client_id is Primary key, Auto increment, not null, lenght 3

2. TypeOfResidence (type_id, name)
type_id is Primary key, Auto increment, not null, lenght 2

3. Order (order_id, FK_client_id, FK_type_id)
order_id is Primary Key, Auto increment, not null, lenght 5
FK_client_id - is foreign key of table Client - client_id
FK_type_id - is foreign key of table TypeOfResidence - type_id

this 2 FK keys is ON DELETE RESTRICT ON UPDATE CASCADE



I tried like this:

For Client:
..
    public class Client : BaseEntity
    {
        public virtual int client_id { get; set; }
        public virtual string name { get; set; }
        public virtual string surname { get; set; }
    }
}
..


public class ClientMap : ClassMap<Client>
    {
        public ClientMap()
        {
        Map(x => x.client_id).Length(11).Not.Nullable();
        Map(x => x.surname).Length(40).Not.Nullable();
        Map(x => x.name).Length(25);
    }
    }



For TypeOfResidence:
..
    public class TypeOfResidence : BaseEntity
    {
        public virtual int type_id { get; set; }
        public virtual string name { get; set; }
    }
}
..


public class TypeOfResidenceMap : ClassMap<TypeOfResidence>
    {
        public TypeOfResidenceMap()
        {
        Map(x => x.type_id).Length(11).Not.Nullable();
        Map(x => x.name).Length(40).Not.Nullable();
    }
    }
....



i think this 2 above is ok, but i dont know how to make this third where are Foreign keys how i subscribe above...

how should look like an entitie and map entitie for Order in my case...?

This post has been edited by nighttrain: 13 July 2011 - 03:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Fluent NHibernate question

#2 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: Fluent NHibernate question

Posted 13 July 2011 - 03:57 AM

maybe like this

public class Order
{
   public virtual Int order_id { get; set; }
   public virtual Client Client { get; set; }
   public virtual TypeOfResidence TypeOfResidence { get; set; }

}

public class OrderMap : ClassMap<Order>
{
   public OrderMap()
   {
     Map( x => x.Id);
     References(x => x.Client);
     References(x => x.TypeOfResidence);
   }
}


this may help
http://wiki.fluentnh.../Fluent_mapping
Was This Post Helpful? 0
  • +
  • -

#3 nighttrain  Icon User is online

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 333
  • Joined: 22-September 10

Re: Fluent NHibernate question

Posted 13 July 2011 - 06:50 AM

I did exactly the same like u Wrote but i add some lines to Client and TypeOfResidence plz look at this is now everythings ok?

..
    public class Client : BaseEntity
    {
        public virtual int client_id { get; set; }
        public virtual string name { get; set; }
        public virtual string surname { get; set; }
        public IList<Order> Orders { get; set; }   // <= added this
    }
}
..


public class ClientMap : ClassMap<Client>
    {
        public ClientMap()
        {
        Map(x => x.client_id).Length(11).Not.Nullable();
        Map(x => x.surname).Length(40).Not.Nullable();
        Map(x => x.name).Length(25);

        HasMany(x => x.Order)                             //<= and this
                .Access.Property()
                .Cascade.All()
                .LazyLoad()
                .KeyColumn("Client_id");
    }
    }



the same in TypeOfResidence ...

is that good for now?

This post has been edited by nighttrain: 13 July 2011 - 06:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 nighttrain  Icon User is online

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 333
  • Joined: 22-September 10

Re: Fluent NHibernate question

Posted 14 July 2011 - 03:08 AM

i tried it but when i creat database and trying to put this above into it - it's ok but it make MYICAM this 3 tables and in order table there are fk keys but their no FK (normally field..) why is that? Anubody could help?
Was This Post Helpful? 0
  • +
  • -

#5 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: Fluent NHibernate question

Posted 14 July 2011 - 04:29 AM

Map(x => x.client_id).Length(11).Not.Nullable();



should be

Id(x => x.client_id).Length(11).Not.Nullable();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1