I am generating the LINQ-to-SQL DataContext and entity classes for a database. The database has several tables, two of which are - [AccountMaster] and [AccountCodes]. There is a foreign key relationship defined between them, with [AccountMaster].AccountNumber being referenced from [AccountCodes].
Adding a LINQ-to-SQL file in VS2008 and dragging these tables onto the DBML design view appropriately generates a collection of AccountNotes within the AccountMaster class. Meanwhile, using SQLMetal to generate the DataContext does not produce any EntitySet collections.
Designer output:
[Table(Name="dbo.A01_AccountMaster")]
public partial class A01_AccountMaster //...
{
//...
private long _AccountNumber;
private EntitySet<A01aAccountNote> _A01aAccountNotes;
//...
}
SQLMetal output:
[Table(Name="dbo.A01_AccountMaster")]
[DataContract()]
public partial class A01_AccountMaster //...
{
//...
private long _AccountNumber;
//...
}
I am following the guide at
http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx
I have tried to first generate the DBML file using SQLMetal, and then generate the DataContext.cs file from the resulting DBML:
sqlmetal.exe /server:srv /database:db /user:usr /password:pwd /sprocs /namespace:AccountContext /context:AccountContext /dbml:AccountContext.dbml /language:csharp /serialization:unidirectional
sqlmetal.exe /sprocs /namespace:AccountContext /context:AccountContext /code:AccountContext.cs /language:csharp /serialization:unidirectional AccountContext.dbml
This does not generate the associations. In fact, inspecting the DBML files from SQLMetal vs. the designer view:
Design View DBML:
<Type Name="A01_AccountMaster">
<!-- ... -->
<Column Name="AccountNumber" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
<Association Name="A01_AccountMaster_A01aAccountNote" Member="A01aAccountNotes" ThisKey="AccountNumber" OtherKey="AccountNumber" Type="A01aAccountNote" />
<!-- ... -->
</Type>
SQLMetal DBML:
<Type Name="A01_AccountMaster">
<!-- ... -->
<Column Name="AccountNumber" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
<!-- ... -->
</Type>
So the association is missing already at the DBML step.
Since the database contains tons of tables/sprocs, it is not practical to use the designer to regenerate the DataContext classes. How do I make SQLMetal generate associations correctly?
EDIT:
Running SQLMetal on the entire database, I realized that SOME entity associations are being generated correctly. The foreign key on AccountNotes is defined as:
ALTER TABLE [dbo].[A01aAccountNotes] WITH CHECK ADD CONSTRAINT [FK_A01aAccountNotes_A01_AccountMaster] FOREIGN KEY([AccountNumber])
REFERENCES [dbo].[A01_AccountMaster] ([AccountNumber])
GO
ALTER TABLE [dbo].[A01aAccountNotes] CHECK CONSTRAINT [FK_A01aAccountNotes_A01_AccountMaster]
GO
EDIT2:
I've noticed that the associations correctly created are those which have an ON DELETE CASCADE/UPDATE rule. Is it then nonsensical to generate associations which do not have this rule strictly defined at the database level?