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 7502309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:49:21+00:00 2026-05-29T20:49:21+00:00

I had thought that this would be a relatively straight forward mapping / persistence

  • 0

I had thought that this would be a relatively straight forward mapping / persistence test but after banging my head this problem I figured I would turn to the experts.

When running the persistence test below I’m running into this error:

NHibernate.Exceptions.GenericADOException : could not insert:
System.Data.SQLite.SQLiteException : SQLite error foreign key mismatch

When reviewing the generated sqllite tables from the test it appears that there is a duplicated foreign key. Unsure of how to remedy this issue, if that is in fact what I’m doing wrong.

Entities:

public class Area
{
    public virtual int AreaID { get; set; }
    public virtual string AreaCode { get; set; }
    public virtual string AreaDescription { get; set; }

    public virtual IList<Location> Locations { get; set; }
}

public class Location {
    public virtual int AreaID { get; set; }
    public virtual string CityName { get; set; }
    public virtual string AreaName { get; set; }
    public virtual Area Area { get; set; }
}

The Mappings:

    public AreaMap()
    {
        Table("Area");
        LazyLoad();
        Id(x => x.AreaID).GeneratedBy.Identity().Column("AreaID");
        Map(x => x.AreaCode).Column("AreaCode").Not.Nullable().Length(2);
        Map(x => x.AreaDescription).Column("AreaDescription").Not.Nullable().Length(50);
        HasMany(x => x.Locations)
            .KeyColumn("AreaCode")
            .Inverse()
            .Cascade.None();
    }

public LocationMap()
{
    Table("Locations");
    LazyLoad();
    Id(x => x.AreaID).GeneratedBy.Assigned().Column("AreaID");
    Map(x => x.CityName).Column("CityName").Length(255);
    Map(x => x.AreaName).Column("AreaName").Length(255);
    References(x => x.Area)
        .PropertyRef(x => x.AreaCode)
        .Column("AreaCode")
        .Fetch.Join();
}

The Test:

    new PersistenceSpecification<Location>(Session)
        .CheckProperty(x => x.AreaID, 1)
        .CheckProperty(x => x.CityName, "SomeCity")
        .CheckProperty(x => x.AreaName, "SomeSubArea")
        .CheckReference(x => x.Area, new Area { AreaCode = "S1", AreaDescription = "Some description goes here" })
        .VerifyTheMappings();

Generated Tables:

create table Area (
        AreaID  integer primary key autoincrement,
       AreaCode TEXT not null,
       AreaDescription TEXT not null
    )

create table Locations (
    AreaID INT not null,
   CityName TEXT,
   AreaName TEXT,
   AreaCode TEXT,
   primary key (AreaID),
   constraint FK6AF881A49C5CF81 foreign key (AreaCode) references Area,
   constraint FK6AF881A49C5CF81 foreign key (AreaCode) references Area (AreaCode)
)
  • 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-05-29T20:49:23+00:00Added an answer on May 29, 2026 at 8:49 pm
    HasMany(x => x.Locations)
        .PropertyRef(x => x.AreaCode)
    

    Update: it could be that area is not saved. Try with

    HasMany(x => x.Locations)
        .Cascade.All()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be a relatively straight-forward question that I just haven't been searching for
I thought I would find a solution to this problem relatively easily, but here
I thought I had this figured out but it turns out I'm just deleting
Alright, I thought I had this whole setTimeout thing perfect but I seem to
Up to now, I thought that if I have RSA-encrypted data, this data would
I long thought that in C, all variables had to be declared at the
I had a wild thought today. I've been discovering that my users like my
This may seem like a programming 101 question and I had thought I knew
I thought I had seen a bug report about this on the jQuery site,
I thought I had a pretty good handle on memory management for objective-c, but

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.