Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8897061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:14:18+00:00 2026-06-15T00:14:18+00:00

I have this complex situation: a database of countries/regions/states/cities which primary key is composed

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:14:19+00:00Added an answer on June 15, 2026 at 12:14 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex layout situation in which an horizontal LinearLayout holds two other
This is somewhat complex (well to me at least). Here is what I have
Hopefully this is less complex than I think. I have one table of companies,
It may well be that I have to write something overly complex for this
I have a complex query that gets executed like this: if ($stmt = $dbi->prepare($pt_query))
I have a very complex Microsoft Access report. This report is run for multiple
I couldn't find this in the documentation. If I have a complex object in
So, The situation is like this: I have one table called invoice, and it
I need to solve a complex situation, where i already have a Canvas element
Here's the situation: I have a website with a complex address form with multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.