Enable serialization for existing LINQ-to-SQL entities used by WCF
An existing WCF service that exposes generated LINQ-to-SQL entities needs a serializable data contract. The DBML designer can generate the required unidirectional serialization metadata.
This is a .NET Framework maintenance workflow for a service that already uses DBML and WCF. For a new API, define explicit transport DTOs instead of exposing persistence entities directly.
Regenerate the entity metadata
Requires the existing DBML project and Visual Studio components for LINQ to SQL on .NET Framework.
-
Open the DBML in the Object Relational Designer.
-
Select the designer surface and set Serialization Mode to Unidirectional.
-
Save and inspect the regenerated entity classes for their data-contract metadata.
-
Rebuild the service and its client contract, then exercise an entity with a related collection.
For an existing SQLMetal-based generation workflow, install the .NET Framework SDK tool and use an SDK command prompt where sqlmetal is available. Pass the existing DBML file as the positional input:
sqlmetal /code:Model.cs /serialization:Unidirectional Model.dbml
Use a disposable output location while checking generation. Do not overwrite a hand-maintained source file. /dbml selects DBML output and cannot be combined with /code. It is not the way to specify the input file. The expected result is generated serialization metadata rather than a manually copied [Serializable] attribute.
Keep the service contract bounded
Inspect which relationships are included and how much data each service call returns. Materialize required values while the data context is alive. Do not let serialization accidentally trigger unbounded lazy loading. Compare client-visible fields after regeneration before changing a deployed service.
Serialization settings solve a graph representation problem, not authorization or update semantics. Validate incoming values and map permitted changes explicitly. A DTO remains a useful boundary when the persistence model should evolve independently of service clients.
Official references


Author: Herman Schoenfeld