2

In our project we have a standard auto-generated designer.cs file, linked to a DBML file, that contains all our object classes that map onto our database tables.

We want to pass these objects directly through a WCF Service and so they need decorating with the [DataContract] and [DataMember] attributes where appropriate. What is the best approach to doing this so the changes won't get wiped out when the designer.cs file is re-generated upon a change to the database scheme or some other change.

Partial classes are an option, but if the property I want to decorate with the DataMember attribute is already defined in the autogenerated designer.cs file then I can't add the same property definition to the partial class as this means the property will have been defined twice.

4

2 回答 2

3

将 DBML 序列化模式设置为单向将使用所需属性装饰类和一些成员,但是它将忽略一些关联以避免在 SP1 之前存在问题的循环引用。

如果您也想要这些,请查看我的LINQ to SQL T4 模板,该模板提供了与 SP1 完全兼容的 DataContract 属性(取消注释 DataClasses.tt 文件中的 data.SerializationMode = DataContractSP1 行)以及让您将 DBML 的任何其他部分自定义为 C# /VB.NET 代码生成过程。

于 2008-09-18T19:44:27.833 回答
0

dbml 文件提供部分类,因此您可以创建一个新的 .cs 文件,定义要扩展的部分类,然后使用所需的属性对其进行装饰。例如,如果您有一个生成的数据上下文,看起来像

public partial class MyDataContext : System.Data.Linq.DataContext
{
...
}

您可以在单独的 .cs 文件中定义以下内容:

[DataContract]
public partial class MyDataContext
{
...
}

这样,您可以扩展生成的类,而不必担心在重新生成 dbml 文件时它们会被覆盖。

于 2008-09-18T15:35:32.797 回答