Are you going off the Fluent Convention or the automap convention? Check
out:http://wiki.fluentnhibernate.org/show/AutoMappingConventions

<http://wiki.fluentnhibernate.org/show/AutoMappingConventions>For the second
question, the answer is to have a bi-directional from assets to vehicle.
Your two options here are as follows:
1.) You can have the collection from the parent to the child maintain a
cascade: All, and whenever an element is added/removed/updated, it will also
cascade those adds/removes/updates in the database for the elements in the
collection
2.) You can have a bi-directional, where the child has a reference back to
the parent. In this scenario, the parent collection is mapped with a
cascade:"all", but also has the "Inverse()" set on it. On the child side,
it's mapped back to the parent as a "References()" fluent mapping, and is
set to cascade: "SaveUpdate". You still remove assets out of your collection
on vehicles, nhibernate behind the scenes just saves the association from
the child to parent direction.

On Wed, Jun 10, 2009 at 11:18 AM, Sarkie <[email protected]> wrote:

>
> Am I missing something or has .WithConventions been removed? All
> tutorials use it, but it isn't my AutoPersistanceModel.
>
> I am also thinking of setting Assets to have a Vehicle_Id or -1 or
> something to say it has no parent any more, but the foreing key
> constraint is then violated, will this be in conventions too?
>
> On Jun 10, 4:35 pm, Sarkie <[email protected]> wrote:
> > Thanks I'll look into it, I think I'll go to mapping now I've got a
> > jist of the setting up of data between my classes etc!
> >
> > Cheers!
> >
> > On Jun 10, 4:23 pm, Hudson Akridge <[email protected]> wrote:
> >
> > > Ah, the cascade needs to be set to all, or all-delete-orphan on the bag
> > > mapping. You can do this by convention on the automapper config (I
> don't
> > > have it off the top of my head, but check into the automap
> conventions). You
> > > want to set the .Cascade.All() or .Cascade.AllDeleteOrphan() on
> collections.
> > > Again, this is assuming you:
> > > 1.) Don't want a bi-directional back from asset to vehicle, in which
> case
> > > you need to declare the collection as inverse, and the reference to the
> > > parent as save-update for persisting
> > > 2.) Don't want to use actual fluent mappings, which if you get any more
> > > complex than this, should probably be the direction you head. It'll
> give you
> > > more control over your mappings, and is an easy step from automapping.
> >
> > > On Wed, Jun 10, 2009 at 10:16 AM, Sarkie <[email protected]> wrote:
> >
> > > > It was doing Save, I changed to SaveOrUpdate and still the same.
> >
> > > > It is the Vehicle_id in the Asset table, i.e. in the Child.
> >
> > > > and here is the config
> >
> > > > Vehicle
> >
> > > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> > > > access="">
> > > >  <class name="ThalesAssetTracking.Core.Entities.Vehicle,
> > > > ThalesAssetTracking.Core, Version=1.0.0.0, Culture=neutral,
> > > > PublicKeyToken=null" table="`Vehicle`" xmlns="urn:nhibernate-
> > > > mapping-2.2">
> > > >    <id name="Id" type="Int32" column="Id">
> > > >      <generator class="identity" />
> > > >    </id>
> > > >    <property name="Name" type="String">
> > > >      <column name="Name" />
> > > >    </property>
> > > >    <property name="Description" type="String">
> > > >      <column name="Description" />
> > > >    </property>
> > > >    <many-to-one name="Image" column="Image_id" />
> > > >    <many-to-one name="DrawingImage" column="DrawingImage_id" />
> > > >    <bag name="CurrentAssets">
> > > >      <key column="Vehicle_id" />
> > > >      <one-to-many class="ThalesAssetTracking.Core.Entities.Asset,
> > > > ThalesAssetTracking.Core, Version=1.0.0.0, Culture=neutral,
> > > > PublicKeyToken=null" />
> > > >    </bag>
> > > >  </class>
> > > > </hibernate-mapping>
> >
> > > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> > > > access="">
> > > >  <class name="ThalesAssetTracking.Core.Entities.Asset,
> > > > ThalesAssetTracking.Core, Version=1.0.0.0, Culture=neutral,
> > > > PublicKeyToken=null" table="`Asset`" xmlns="urn:nhibernate-
> > > > mapping-2.2">
> > > >    <id name="Id" type="Int32" column="Id">
> > > >      <generator class="identity" />
> > > >    </id>
> > > >    <property name="Name" type="String">
> > > >      <column name="Name" />
> > > >    </property>
> > > >    <property name="Number" type="String">
> > > >      <column name="Number" />
> > > >    </property>
> > > >    <many-to-one name="AssetType" column="AssetType_id" />
> > > >  </class>
> > > > </hibernate-mapping>
> >
> > > > I am guessing the settings for the one-to-many is wrong?
> >
> > > > Cheers, Sarkie.
> >
> > > > On Jun 10, 3:54 pm, Hudson Akridge <[email protected]> wrote:
> > > > > In your vehicles repository, is the .Add(veh1) calling the
> > > > > session.SaveOrUpdate(veh1)? If so, then is the Vehicle_Id column
> you're
> > > > > describing as being empty in the vehicles table or the assets
> table?
> >
> > > > > Could you do an .ExportTo() in the config and post the vehicle and
> asset
> > > > > mappings that automapper is generating for you? It's possible that
> it's
> > > > > setting the collection side as an inverse (in NHibernate lingo it
> means
> > > > it
> > > > > won't save from the parent side of the relationship, it expects the
> child
> > > > to
> > > > > save the association) You might also try doing this as a
> bi-directional
> > > > by
> > > > > adding a reference back to vehicle from asset (although you
> shouldn't
> > > > have
> > > > > to by default).
> >
> > > > > On Wed, Jun 10, 2009 at 5:27 AM, Sarkie <[email protected]> wrote:
> >
> > > > > > Hi Guys,
> >
> > > > > > Just started a new project and decided I wanted to give
> Nhibernate a
> > > > > > try after trying out Entity Framework, I was told by everyone to
> use
> > > > > > Fluent instead( as well).
> >
> > > > > > So I am just trying to develop a simple app and I've come across
> an
> > > > > > issue with the AutoMapping, reading the Examples.FirstProject it
> > > > > > *should* work, but I am using AutoMapping rather than manually
> doing
> > > > > > it.
> >
> > > > > > Here is my Parent - Vehicle:
> >
> > > > > > public class Vehicle
> > > > > >    {
> > > > > >        public virtual int Id { get; private set; }
> > > > > >        public virtual Image Image { get; set; }
> > > > > >        public virtual Image DrawingImage { get; set; }
> > > > > >        public virtual string Name { get; set; }
> > > > > >        public virtual string Description { get; set; }
> >
> > > > > >        public virtual IList<Asset> CurrentAssets {get; private
> set;}
> >
> > > > > >        public Vehicle()
> > > > > >        {
> > > > > >            CurrentAssets = new List<Asset>();
> > > > > >        }
> >
> > > > > >        public virtual void AddAsset(Asset asset)
> > > > > >        {
> > > > > >            CurrentAssets.Add(asset);
> > > > > >        }
> >
> > > > > >    }
> >
> > > > > > Here is my Child - Asset
> >
> > > > > > public class Asset
> > > > > >    {
> > > > > >        public virtual int Id { get; private set; }
> > > > > >        public virtual string Name { get; set; }
> > > > > >        public virtual string Number { get; set; }
> > > > > >    }
> >
> > > > > > Here is my configuration
> >
> > > > > > FluentConfiguration fs = Fluently.Configure()
> > > > > >                        .Database(MsSqlConfiguration.MsSql2005
> > > > > >                        .ConnectionString(c => c
> > > > > >                            .Server("servername")
> > > > > >                            .Database("dbname")
> > > > > >                            .Username("us")
> > > > > >                            .Password("pw")))
> > > > > >                        .Mappings(m =>
> > > > > >                            m.AutoMappings.Add(
> > > > > >                            AutoPersistenceModel
> > > > > >                            .MapEntitiesFromAssemblyOf<Asset>()
> > > > > >                            .Where(t => t.Namespace ==
> > > > > > "AssetTracking.Core.Entities")));
> >
> > > > > > I then do the insertion:
> >
> > > > > >            UnitOfWork.UnitOfWork.Start();
> >
> > > > > >            Repository<Vehicle> vehicles = new
> Repository<Vehicle>();
> >
> > > > > >            var veh1Asset = new Asset
> > > > > >            {
> > > > > >                AssetType = assetType,
> > > > > >                Name = "Asset 1",
> > > > > >                Number = "123",
> >
> > > > > >            };
> >
> > > > > >            //Repository<Asset> assets = new Repository<Asset>();
> > > > > >            //assets.Add(veh1Asset);
> >
> > > > > >            //List<Asset> assList = new List<Asset>();
> > > > > >            //assList.Add(veh1Asset);
> >
> > > > > >            // add 2 vehicles
> > > > > >            var veh1 = new Vehicle
> > > > > >            {
> > > > > >                Description = "Truck 101, this is a long standing
> blah
> > > > > > blah blah",
> > > > > >                Name = "Truck 101",
> > > > > >                Image = veh1Image,
> > > > > >                DrawingImage = vehImage2,
> >
> > > > > >            };
> >
> > > > > >            veh1.AddAsset(veh1Asset);
> >
> > > > > >            vehicles.Add(veh1);
> > > > > >        }
> >
> > > > > > Now I've tried adding it different ways before and after and in
> any
> > > > > > case I try, the Vehicle_Id is always null in the DB.
> >
> > > > > > Am I missing something?
> >
> > > > > > Sorry if I've missed something simple!!
> >
> > > > > > Cheers, Sarkie.
> >
> > > > > --
> > > > > - Hudsonhttp://www.bestguesstheory.comhttp://
> twitter.com/HudsonAkridge
> >
> > > --
> > > - Hudsonhttp://www.bestguesstheory.comhttp://twitter.com/HudsonAkridge
> >
>


-- 
- Hudson
http://www.bestguesstheory.com
http://twitter.com/HudsonAkridge

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to