2012-02-14 38 views
0

我原以为这将是一个相对直接的映射/持久性测试,但在撞我的头后,我想我会转向专家。Fluent-Nhibernate持久性规范测试生成SQLite错误外键不匹配

当运行持续性检验下面,我遇到了此问题:

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

在审查从测试生成的sqllite表似乎是有重复的外键。不确定如何解决这个问题,如果这实际上是我做错了。

实体

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; } 
} 

的映射

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

测试

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

个生成的表格

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
HasMany(x => x.Locations) 
    .PropertyRef(x => x.AreaCode) 

更新:它可能是该区域不会被保存。用

HasMany(x => x.Locations) 
    .Cascade.All() 
+0

纠正重复的外键问题。但是,不匹配错误仍在发生? – Jesse 2012-02-14 16:56:19