Hellraiser
Hellraiser

Reputation: 609

Fluent NHibernate automapping composite id with component

I have this complex situation: a database of countries/regions/states/cities which primary key is composed by a code (nvarchar(3)) in a column called "Id" plus all key columns of "ancestors" (regions/states/cities).

So the table country has only one key coumn (Id) while cities has 4 key columns (Id, StateId,regionId,CountryId). Obviously they're all related, so each ancestor column is a foreign key to the related table.

I have Entities in my Model that map this relationships. But they all derive from one type called Entity<T> where T may be a simple type (string, in etc) or a complex one (a component implementing the key). Entity<T> implements a single property called Id of type T.

For each db table, if it has a comlex key, I implement it in a separate component, which oveerides also Equals and GetHashCode() Methods (in future I'll implement those in the Entity base class).

So I have a RegionKey componet that has 2 properties (Id and CountryId). I have conventions for Foreign Key and primary key naming and type and that is ok. I have also Mapping ovverrides for each complex Entity.

For simplicity, lets concentrate only on Countries and Regions table. Here they are:

public class Country: Entity<string>
{
    public virtual string Name { get; set; }
    public virtual IList<Region> Regions { get; set; } 
}

 public class Region: Entity<RegionKey>
{
    public virtual string Name { get; set; }
    public virtual Country Country { get; set; }
}

and the RegionKey component:

namespace Hell.RealHellState.Api.Entities.Keys
{
  [Serializable]
  public class RegionKey
  {
    public virtual string Id { get; set; }
    public virtual string CountryId { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as RegionKey;
        if (t == null)
            return false;
        return Id == t.Id && CountryId == t.CountryId;
    }

    public override int GetHashCode()
    {
        return (Id + "|" + CountryId).GetHashCode();
    } 
  }
}

Here is the configuration of AutoPersistenceModel:

    public ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
          .Database(
            MsSqlCeConfiguration.Standard
            .ConnectionString(x=>x.Is(_connectionString))
          )
          .Mappings(m => m.AutoMappings.Add(AutoMappings))
          .ExposeConfiguration(BuildSchema)
          .BuildSessionFactory();
    }

    private AutoPersistenceModel AutoMappings()
    {
        return AutoMap.Assembly(typeof (Country).Assembly)
            .IgnoreBase(typeof(Entity<>))
            .Conventions.AddFromAssemblyOf<DataFacility>()
            .UseOverridesFromAssembly(GetType().Assembly)
            .Where(type => type.Namespace.EndsWith("Entities"));
    }

    private static void BuildSchema(Configuration config)
    {
        //Creates database structure
        new SchemaExport(config).Create(false, true);
        //new SchemaUpdate(config).Execute(false, true);
    }

Here is the Regions entity overrides

public class RegionMappingOverride : IAutoMappingOverride<Region>
{
    public void Override(AutoMapping<Region> mapping)
    {
        mapping.CompositeId(x=>x.Id)
            .KeyProperty(x => x.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
            .KeyProperty(x => x.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
    }
}

Ok now when I test this mapping I got an error saying: The data types of the columns in the relationship do not match.

I have also tried this override:

    public void Override(AutoMapping<Region> mapping)
    {
        mapping.CompositeId()
            .ComponentCompositeIdentifier(x=>x.Id)
            .KeyProperty(x => x.Id.Id, x=> x.ColumnName("Id").Length(3).Type(typeof(string)))
            .KeyProperty(x => x.Id.CountryId, x => x.ColumnName("CountryId").Length(3).Type(typeof(string)));
    }

And it almost work but it creates a Regions table with a single column key of varbinary(8000) which is not what I want:

CREATE TABLE [hell_Regions] (
[Id] varbinary(8000) NOT NULL
, [Name] nvarchar(50) NULL
, [CountryId] nvarchar(3) NULL
);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [PK__hell_Regions__0000000000000153] PRIMARY KEY ([Id]);
GO
ALTER TABLE [hell_Regions] ADD CONSTRAINT [FK_Regions_Country] FOREIGN KEY ([CountryId]) REFERENCES [hell_Countries]([Id]) ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

I don't have a clue of how to deal with it since it seems to me everythin is ok.

Thanks in advance for your answers

Upvotes: 3

Views: 3223

Answers (1)

Hellraiser
Hellraiser

Reputation: 609

Ok I menaged to solve it: I had to sign the CompositeId class as MAPPED, since it is a component. So this is my new RegionMappingOverride:

public class RegionMappingOverride : IAutoMappingOverride<Region>
{
    public void Override(AutoMapping<Region> mapping)
    {
        mapping.CompositeId(x=>x.Id)
            .Mapped()
            .KeyProperty(x =>x.Id,x=>x.Length(3))
            .KeyProperty(x => x.CountryId, x=>x.Length(3));
    }

}

Now the sql created is correct:

create table hell_Countries (
    Id NVARCHAR(3) not null,
   Name NVARCHAR(50) null,
   primary key (Id)
)

create table hell_Regions (
    Id NVARCHAR(3) not null,
   CountryId NVARCHAR(3) not null,
   Name NVARCHAR(50) null,
   primary key (Id, CountryId)
)

alter table hell_Regions 
    add constraint FK_Region_Country 
    foreign key (CountryId) 
    references hell_Countries

Upvotes: 4

Related Questions