views:

209

answers:

1

Why is SqlMetal messing up the Association names. For e.g. in my 'TextMessage' table i have two columns referencing the 'ApplicationUser' table. 'SenderUserId' and 'RecipientUserId'

When I run SqlMetal and look at my 'ApplicationUser' class

For 'RecipientUserId' it generates:

[Association(Name="FK__TextMessa__Recip__72910220", Storage="_TextMessages", ThisKey="Id", OtherKey="RecipientUserId", DeleteRule="NO ACTION")]
     public EntitySet<TextMessage> TextMessages
     {
      get
      {
       return this._TextMessages;
      }
      set
      {
       this._TextMessages.Assign(value);
      }
     }

and for 'SenderUserId' it generates this garbage named property:

[Association(Name="FK__TextMessa__Sende__73852659", Storage="__TextMessa__Sende__73852659s", ThisKey="Id", OtherKey="SenderUserId", DeleteRule="NO ACTION")]
     public EntitySet<TextMessage> _TextMessa__Sende__73852659s
     {
      get
      {
       return this.@__TextMessa__Sende__73852659s;
      }
      set
      {
       this.@__TextMessa__Sende__73852659s.Assign(value);
      }
     }

How can I remedy this? This is unusable. Is there a better way to generate Linq To Sql Code???

+1  A: 

You can instruct SqlMetal to generate a DBML file:

SqlMetal /server:myserver /database:northwind /dbml:northwind.dbml /namespace:nwind

and then correct the association names in the DBML file and then generate from the DBML:

SqlMetal /code:nwind.cs /map:nwind.map northwind.dbml

The only problem with doing this is that if you re-generate the DBML after updating your database, any changes to your DBML will wash out.

Other options:

  • Use Visual Studio's designer (not great if your schema is large)
  • Search for a third-party tool to generate DataContexts
  • Write your own tool

One further point: I've rarely seen SqlMetal emit an association name that bad. How are your columns named? Is there a conflict with another relationship name?

Joe Albahari