Dr. Paul Jarvis
Dr. Paul Jarvis

Reputation: 256

Fluent NHibernate IList<string>

I have the following class:

public class Address
{
    public virtual int Id { get; private set; }

    public virtual IList<string> AddressLines { get; set; }

    public virtual string CityOrTown { get; set; }

    public virtual string County { get; set; }

    public virtual string Postcode { get; set; }

    public virtual string Country { get; set; }

    public virtual Member Member { get; set; }

    public Address()
    {
        AddressLines = new List<string>();
    }

    public virtual void AddAddressLine(string addressLine)
    {
        AddressLines.Add(addressLine);
    }
}

and the following mapping class:

public AddressMap()
{
    Id(x => x.Id);
    HasMany(x => x.AddressLines).Element("AddressLine");
    Map(x => x.CityOrTown);
    Map(x => x.County);
    Map(x => x.Postcode);
    Map(x => x.Country);
    References(x => x.Member);
}

The table 'addresslines' is created with two columns 'Address_Id' and 'AddressLine' with a Foreign Key to the 'address' table.

For some reason when an address is saved the 'AddressLines' collection is not persisted to the database;

Am I missing something?

There doesn't seem to be a great deal of information out there about mapping IList. I am aware that this didn't use to be possible because string is value type not an entity type, but I am led to believe this should now be possible?

Upvotes: 0

Views: 1822

Answers (2)

Firo
Firo

Reputation: 30803

HasMany(x => x.AddressLines).Element("AddressLine").Cascade.AllDeleteOrphan();

Upvotes: 3

Tom Bushell
Tom Bushell

Reputation: 5875

You need to set a default cascade convention.

When using Automapping, it's done as follows:

            AutoMap.Assemblies(_assemblies)
                .Conventions.Add(
                    // Do cascading saves on all entities so lists  will be
                    // automatically saved 
                    DefaultCascade.All(),
                );

As far as I know, you can do the same thing with mapping classes, but I don't know the exact syntax.

Upvotes: 1

Related Questions