foreign keys support

Editor
Jul 1, 2011 at 5:52 PM

In source code I see that there is "some kind" of foreign key support. Could you give some example how to use it?

Thanks.

Coordinator
Jul 1, 2011 at 6:18 PM

The only caveat about the foreign key support is it wont verify that the relationship exists.  That is  if the column needs to be added and it has a foreign key relationship defined it will add it. However, if the column is already there it assums the relationship is in place. With that said adding one is easy enough..


First define your primary key type

 

public class TestItemPrimaryKey
{
	public int ID { get; set; }
	public string Name { get; set; }
  public class TestItemWithForeignKey
  {
    public int id { get; set; }
    public string Something { get; set; }

    [DataField(PrimaryKeyType=typeof(TestItem), RelationshipType = ForeignKeyType.SetNull)]
    public int ForeignKey { get; set; }
  }
public string Date { get; set; } }

Then define your foreign key relationship
public class TestItemWithForeignKey
{
  public int id { get; set; }
  public string Something { get; set; }

[DataField(PrimaryKeyType=typeof(TestItemPrimaryKey))] public int ForeignKey { get; set; } }

You can also define what action to take on delete (default is cascade)
public class TestItemWithForeignKey
{
  public int id { get; set; }
  public string Something { get; set; }

  [DataField(PrimaryKeyType=typeof(TestItem), RelationshipType = ForeignKeyType.SetNull)]
  public int ForeignKey { get; set; }
}