Foreign Key Generation

Apr 17, 2014 at 8:30 AM
Apologies for all the questions I keep firing at you. I think you've done a great job on the ORM and am finding it invaluable in my project.

Just a quick question on referencing. If I add a reference like:
[Reference(typeof(Tabel1), "ID", "TableID", ReferenceType = ReferenceType.ManyToOne)]
No foreign key is added to the table schema. Is this intentional? Do you think it would be a good idea to add this? I could have a crack at it myself. Thanks.
Apr 17, 2014 at 6:31 PM
The Entity definition needs to have the ID in it as a PK to have the field auto-generated, or am I not understanding your question?
Apr 24, 2014 at 12:35 PM
I'm talking about a foreign key in the database schema (in this case SqlCE). If I have two tables defined as:
    [Entity(KeyScheme = KeyScheme.Identity)]
    public class Table1
        [Field(IsPrimaryKey = true)]
        public int ID { get; set; }

        [Reference(typeof(Table2), "ID", "ID", ReferenceType = ReferenceType.OneToMany)]
        public Table2 Table2 { get; set; }
    [Entity(KeyScheme = KeyScheme.None)]
    public class Table2
        [Field(IsPrimaryKey = true)]
        public int ID { get; set; }

        [Reference(typeof(Table1), "ID", "ID", ReferenceType = ReferenceType.ManyToOne)]
        public Table1 Table1 { get; set; }
I actually want a one-to-one relationship, but there is no provision for this, so I have chosen OneToMany. When I create the database, no foreign keys are generated in the database.
Apr 24, 2014 at 2:44 PM
Ah, so you're looking for an actual Foreign Key constraint to be created in the database? Currently the ORM doesn't generate them because I didn't see the actual need or value. If there is value in it, I can certainly look at adding it. What benefit does the constraint give you?

Right now the Reference attribute simply tells ORM that there is associated data to the Entity and if a Select is made that requests FillReferences, it then knows where to go get that reference entity information. So it's a semantic foreign key relationship to ORM, but it doesn't actually generate any physical constraint in the database for it.
Apr 25, 2014 at 9:40 AM
Just for data integrity because without it, it would be possible to add a record with a foreign key that doesn't exist.

I agree it is low priority though. Thanks.