Jesse
Jesse

Reputation: 8393

Fluent-Nhibernate Persistence Specification test generating SQLite error foreign key mismatch

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)
)

Upvotes: 0

Views: 653

Answers (1)

Firo
Firo

Reputation: 30803

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()

Upvotes: 1

Related Questions