简体   繁体   English

流畅的映射SQLite中的奇怪外键约束

[英]Strange Foreign Key constraint in fluent mapped SQLite

I have a model written using FluentNhibernate and I am trying to create some test data in an in-memory SQLite database. 我有一个使用FluentNhibernate编写的模型,并且尝试在内存SQLite数据库中创建一些测试数据。

var fConfig =
                Fluently.Configure()
                        .Database(config)
                        .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetAssembly(exampleClass))
                        .Conventions.Add(AutoImport.Never())
                        .Conventions.Add(new SQLiteGeometryTypeConvention())
                         .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never()))
                        .ExposeConfiguration(cfg => configuration = cfg);

The problem I am getting is that one of the tables is created like this in the generated sql something like this : 我得到的问题是在生成的sql中像这样创建表之一:

 create table myTable (
       myID  integer primary key autoincrement,
       ...{skip normal columns} 
       ForeignTable1ID INT,
       ParentID BIGINT,
       constraint FK1A2E045AFEC6908F foreign key (ForeignTable1ID) references ForeignTable1,
       constraint FK1A2E045ABB4EBD1F foreign key (ParentID) references Parent,
       constraint FKE21911CF6853D06E foreign key (myID) references Parent
)

I don't want that third constraint but don't know what's causing it! 我不想第三个约束,但是不知道是什么原因造成的! The effect being that I can only create records in myTable which have valid foreign keys BUT ALSO have a myID value which exists in the Parent table. 结果是我只能在myTable中创建具有有效外键的记录,但同时在Parent表中也存在myID值。 This is unnecessary, and I can't see what's causing it. 这是不必要的,我看不到是什么原因造成的。

The mapping file looks like this: 映射文件如下所示:

Table("dbo.PalslagInventering");
            Id(x => x.myId).Column("myID");
            References(x => x.ForeignTable1).Column("ForeignTable1ID");
            References(x => x.Parent).Column("ParentID");

            Map(x => x.{other columns here});

HasMany(x => x.Child).KeyColumn("myID");
            HasOne(x => x.Child2).ForeignKey("MyID");

            HasMany(x => x.Child2).KeyColumn("MyID").ForeignKeyConstraintName("Child2");
            HasMany(x => x.Child3).KeyColumn("MyID").ForeignKeyConstraintName("Child3");

The parent table (being referenced) has a simple 父表(被引用)有一个简单的

public virtual IList<MyTable> myTableRecords { get; private set; }

Type of code mapped as: 映射为的代码类型:

HasMany(x => x.myTableRecords)
                .KeyColumn("myID")
                .Inverse();

What is causing the "foreign key" reference back to it's own myId? 是什么导致“外键”引用返回到其自身的myId?

The error seems to be: 错误似乎是:

HasMany(x => x.myTableRecords)
                .KeyColumn("myID")
                .Inverse();

Where it's the Child table (ie "MyTable") which has the ID "myID" and the KeyColumn should point to the Parent table (ie the foreign key in "MyTable"). 子表(即“ MyTable”)的ID为“ myID”,KeyColumn应该指向父表(即“ MyTable”中的外键)。

So: 所以:

HasMany(x => x.myTableRecords)
                .KeyColumn("ParentID")
                .Inverse();

Seems to have fixed the problem. 似乎已解决问题。 That the error was in the parent class's mapping file meant I missed it at first. 该错误是在父类的映射文件中表示我一开始就错过了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM