Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2004
    Location
    UK
    Posts
    370

    Question MVC : Problems Creating Mapping (nHibernate)

    Hi,

    First of all I would like to thank Nelson for a wonderful course on ASP.Net MVC - I learnt so much from that course and am continuing to educate myself in MVC.

    The problem I am having is creating a mapping relationship between a table of countrys, and a table of employees (all just test data for practice). Basically each employee record will have a country id associated with them.

    Is this a ManyToOne relationship? And if so, how do I code it?

    So far I have:

    Code:
     public class Employee
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual int CountryId { get; set; }
        }
    
        public class EmployeeMap : ClassMapping<Employee>
        {
            public EmployeeMap()
            {
                Table("employees");
    
                Id(x => x.Id, x => x.Generator(Generators.Identity));
    
                Property(x => x.Name, x => x.NotNullable(true));
    
                ManyToOne(x => x.CountryId, x => 
                    {
                        x.Column("id");
                    });
            }
        }
    But this fails to compile, saying that CountryID must be a reference type to use it as a parameter, and I guess I need to somehow reference the department table in there somewhere also.

    I can supply the table definitions and department mappings if needed.

    Any help would be greatly appreciated.

    Thanks in advance, Ian.


    XNA Tip #1: Take it easy on the garbage collector, and your performance will thank you.

  2. #2
    Join Date
    Mar 2004
    Location
    Anacortes, WA
    Posts
    4,168
    Quote Originally Posted by errolian View Post
    Hi,

    First of all I would like to thank Nelson for a wonderful course on ASP.Net MVC - I learnt so much from that course and am continuing to educate myself in MVC.

    The problem I am having is creating a mapping relationship between a table of countrys, and a table of employees (all just test data for practice). Basically each employee record will have a country id associated with them.

    Is this a ManyToOne relationship? And if so, how do I code it?

    So far I have:

    Code:
     public class Employee
        {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
            public virtual int CountryId { get; set; }
        }
    
        public class EmployeeMap : ClassMapping<Employee>
        {
            public EmployeeMap()
            {
                Table("employees");
    
                Id(x => x.Id, x => x.Generator(Generators.Identity));
    
                Property(x => x.Name, x => x.NotNullable(true));
    
                ManyToOne(x => x.CountryId, x => 
                    {
                        x.Column("id");
                    });
            }
        }
    But this fails to compile, saying that CountryID must be a reference type to use it as a parameter, and I guess I need to somehow reference the department table in there somewhere also.

    I can supply the table definitions and department mappings if needed.

    Any help would be greatly appreciated.

    Thanks in advance, Ian.
    Glad to hear you enjoyed the series

    With mapping a ManyToOne, what you're doing is mapping one entity to another. That means that the type of the property (in this case the CountryId property) must be the type of the mapped entity you want to reference. Since the type of "CountryId" is an int, it isn't an entity and cannot be mapped as a many to one (or any other kind of mapping other than a simple property).

    Now - you can certainly not map Countries with nHibernate. In that case, you'd map CountryId as a simple property (and make sure to setup the foreign constraint in the database!). However, that may not be what you want since it would not be possible to get the country name from the ID.

    If you want to model this in nHibernate, you'll also need to create a Country class - and map it as an nHibernate entity. Then you'll want to change your
    Code:
    public virtual int CountryId { get; set; }
    to
    Code:
    public virtual Country Country { get; set; }
    And the mapping should work

    Let me know if this makes sense!
    Need any help? Feel free to PM me - or send an email directly to nelson@3dbuzz.com!

  3. #3
    Join Date
    Sep 2004
    Location
    UK
    Posts
    370

    Thanks.

    Hi Nelson,

    Thanks for the reply, it makes perfect sense. So basically I need to stop thinking in terms of database stores and more in terms of objects/entities. So I map to the Country entity which in turn will allow me to access all of the Country properties.

    I'm sure thee may be further questions down the road as I continue my journey.

    Any plans to do more training in MVC, ASP.Net or JQuery in the future?

    Regards, Ian.


    XNA Tip #1: Take it easy on the garbage collector, and your performance will thank you.

  4. #4
    Join Date
    Jan 2014
    Posts
    3
    Something's up with the code i wonder..

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •