Is there an ability to specify foreign key mappings?

Dec 20, 2014 at 4:14 AM
Hi Roman,

I'm trying to recreate the Northwind database with VITA verbatim, at least as much as possible. My main hitch is with foreign keys.

Consider the following for IEmployeeTerritory:
    [Entity(Name="EmployeeTerritory", PluralName="EmployeeTerritories", TableName="EmployeeTerritories")]
    [PrimaryKey("EmployeeID,TerritoryID")]
    [Unique("EmployeeID,TerritoryID", IndexName="PK_EmployeeTerritories")]
    [Paged]
    [OrderBy("TerritoryID")]
    public partial interface IEmployeeTerritory
    {
        int EmployeeID
        {
            get;
            set;
        }
    
        [Size(20)]
        string TerritoryID
        {
            get;
            set;
        }

        IEmployee Employee
        {
            get;
            set;
        }

        ITerritory Territory
        {
            get;
            set;
        }
    }
What I want is to have EmployeeID and TerritoryID to form the primary key, and each of those columns as foreign key references to the corresponding Employee and Territory tables. The above code creates extra Employee_EmployeeID and Territory_TerritoryID columns. I was looking for attributes on Employee and Territory to specify some kind of mapping, but didn't find any. I also tried removing EmployeeID and TerritoryID, but then that caused errors trying to define a primary key with Employee and Territory.

What is the best way to set this up, ideally with the column names I want, or otherwise if I can't.

Thanks!
Coordinator
Dec 20, 2014 at 4:56 AM
You can do it easily. The simplest way - run vdbtool to make VITA generate code for NWind.
Use nwind.vdb.cfg config file - just fix the connection string, then set the cmd line parameters as:
/op:srcgen /c:nwind.vdb.cfg


This will generate file with entities for NWind
In general, you don't specify FK columns explicitly - you specify just references. But you can provide names of underlying FK columns using EntityRef attribute.
For composite primiry key, use PrimaryKey attr on entity and list names of columns that form the PK:

[Entity, PrimaryKey("Employee,Territory")]

Look at book sample, IBookAuthor entity.
Coordinator
Dec 20, 2014 at 4:57 AM
ah, make sure you refresh the code, I just pushed a tiny fix for vdbtool, for a bug I discovered when trying to run vdbtool
Dec 20, 2014 at 2:32 PM
Great, I'll run the tool, thanks Roman!