views:

31

answers:

1

Hello, is it possible to change the foreign key relationship object naming of LINQ to SQL generated objects?

For example, I have a Demo table which has the fields ItemMinId, ItemMaxId, ItemExlId which are all foreign key references to my Item table.

LINQ generates now Demo.Item, Demo.Item2, Demo.Item3 fields which makes it difficult in code to distinguish between the actual items.

How can I change this naming, outside of the DBML as I'm usually refreshing the entire model by deleting everything and dragging the updated SQL tables onto the LINQ designer surface.

A: 

To keep your name across updates, you have to move it to partial classes.

The trick is to declare new properties that simply get/set the generated item.

public partial class Demo
{
    public int ItemMinId
    {
        get { return Item; }
        set { Item = value; }
    }

    public int ItemMaxId
    {
        get { return Item2; }
        set { Item2 = value; }
    }

    pubilc int ItemExlId
    {
        get { return Item3; }
        set { Item3 = value; }
    }
}

You can change the scope of the generated items from public to internal, but I imagine that will be overwritten on each update too.

Properties of Demo that you declare in a separate, partial class, though, will not be changed.

Jay
I can't test this right now, but I don't feel like this would play well with the actual LINQ to SQL engine which generates the SQL to run on the database...
tster
No, it will not.
DamienG