views:

61

answers:

1

How can i create a 3 table schema from the following model classes.

public class Product
{
  public int Id {get; set;}
  public string Name {get; set;}
  public IList<Photo> Photos {get; set;}
}

public class Photo
{
  public int Id {get; set;}
  public string Path {get; set;}
}

I want to create the following table structure in the database:

Product
-------
Id
Name

ProductPhotos
-------------
ProductId (FK Products.Id)
PhotoId (FK Photos.Id)

Photos
------
Id
Path

How i can express the above Database Schema using Fluent NHibernate? I could only manage the following the Mapping but this does not get me the 3rd Photo ref table.

public class ProductMap : ClassMap<Product>
    {
        public ProductMap()
        {  
            Id(x => x.Id);
            Map(x => x.Name);            
            Table("Products");
            HasMany(x => x.Photos).Table("ProductPhotos").KeyColumn("ProductId");
        }
    }
+1  A: 

You have to create a classmap for the photo entity as well.

Paco
Tried this suggestion but no good. Could you advise further of what the mapping details are?
public class PhotoMap : ClassMap<Photo>{ Id(x => x.Id); Map(x => x.Path);}You might need to specify the collection type (bag or list) and cascade as well in the hasmany in the product map. Citymap is a typo?
Paco
Do i need to create a 3rd ClassMap called ProductPhotoMap? Your suggestion does not seem to work
No you don't have to create a 3th classmap. When it doesn't work, the problem must be somewhere else.
Paco
You need a `HasManyToMany` to specify that a join table should be used.
James Gregory
James is right!
Paco