none
Model generation with Linq 2 SQL RRS feed

  • Question

  • When or Linq 2 SQL generates classes for tables in a DB, the property names for the associated entities are a little confusing e.g. I have Customer table and a Customer Address table. We store only 2 addresses for a Customer, current address and previous address - therefore there are two 1:1 associations between the customer table and customer address table. When I import these two tables into my project with LINQ 2 SQL designers, it creates a customer class with 2 properties CustomerAddress and CustomerAddress1 which is exactly what one would expect. However the names are a little wierd. It would be perfect if it would have generated CurrentAddress and PreviousAddress properties. I was wondering how it comes up with names for associated properties. I would love to fix my database tables so that everytime I pull these tables into my project it would generate the correct property names instead of CustomerAddress and CustomerAddress1
    Tuesday, August 18, 2009 6:27 PM

Answers

  • Just to add to Damien's reply: the control over names is very limited with the built in features, but there are third party solutions that give you more options. One such option is my add-in for Visual Studio that adds renaming/naming rules, sync-model-with-db and other useful stuff to the L2S designer.

    When it comes to multiple parallell FK constraints, it will append the names of the fields involved if there are two or more associations/FKs between the same two tables (when using the update model from db feature).

    E.g.:




    ...if your db schema is:

    create table country (country_code nvarchar(5) not null,
      country_name nvarchar(255),
      constraint pk_country primary key (country_code));
    go
    
    create table [address] (address_id int not null identity,
      address_line_1 nvarchar(255),
      address_line_2 nvarchar(255),
      postal_code nvarchar(20),
      city nvarchar(255), 
      country_code nvarchar(5),
      constraint pk_address primary key (address_id),
      constraint fk_address_country foreign key (country_code) references country (country_code));
    go
    create index ix_address_country on [address] (country_code);
    go
    
    create table customer (customer_id int not null identity,
      customer_name nvarchar(255),
      current_address_id int,
      previous_address_id int,
      constraint pk_customer primary key (customer_id),
      constraint fk_customer_current_address foreign key (current_address_id) references [address] (address_id),
      constraint fk_customer_previous_address foreign key (previous_address_id) references [address] (address_id))
    go
    create index ix_customer_current_address on customer (current_address_id);
    create index ix_customer_previous_address on customer (previous_address_id);

    ...and another option might be to use code generation templates and adjust in the template how navigation properties are named...


    .
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Zhipeng Lee Tuesday, August 25, 2009 2:20 AM
    Wednesday, August 19, 2009 1:56 AM
    Answerer

All replies

  • There are unfortunately no rules of mechanisms to adjust the default naming used for associations when generating the DBML.

    [)amien
    Tuesday, August 18, 2009 8:18 PM
    Moderator
  • Just to add to Damien's reply: the control over names is very limited with the built in features, but there are third party solutions that give you more options. One such option is my add-in for Visual Studio that adds renaming/naming rules, sync-model-with-db and other useful stuff to the L2S designer.

    When it comes to multiple parallell FK constraints, it will append the names of the fields involved if there are two or more associations/FKs between the same two tables (when using the update model from db feature).

    E.g.:




    ...if your db schema is:

    create table country (country_code nvarchar(5) not null,
      country_name nvarchar(255),
      constraint pk_country primary key (country_code));
    go
    
    create table [address] (address_id int not null identity,
      address_line_1 nvarchar(255),
      address_line_2 nvarchar(255),
      postal_code nvarchar(20),
      city nvarchar(255), 
      country_code nvarchar(5),
      constraint pk_address primary key (address_id),
      constraint fk_address_country foreign key (country_code) references country (country_code));
    go
    create index ix_address_country on [address] (country_code);
    go
    
    create table customer (customer_id int not null identity,
      customer_name nvarchar(255),
      current_address_id int,
      previous_address_id int,
      constraint pk_customer primary key (customer_id),
      constraint fk_customer_current_address foreign key (current_address_id) references [address] (address_id),
      constraint fk_customer_previous_address foreign key (previous_address_id) references [address] (address_id))
    go
    create index ix_customer_current_address on customer (current_address_id);
    create index ix_customer_previous_address on customer (previous_address_id);

    ...and another option might be to use code generation templates and adjust in the template how navigation properties are named...


    .
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Zhipeng Lee Tuesday, August 25, 2009 2:20 AM
    Wednesday, August 19, 2009 1:56 AM
    Answerer
  • Thanks Kristofer. That is pretty cool. I was thinking of writing a tool myself. Good to hear that there is one already.
    Wednesday, August 19, 2009 3:32 AM
  • Thanks Kristofer. That is pretty cool. I was thinking of writing a tool myself. Good to hear that there is one already.

    Yup, you can download it from http://www.huagati.com/dbmltools/ and get a free 45-day trial license.

    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Wednesday, August 19, 2009 3:44 AM
    Answerer