使用SPMetal 生成的Entities文件,包含中文列表名称,或者包含空格,使用起来很不方便。
找了一番,找到了解决方法。
SPMetal 有个Parameters 参数,可以指定重写的XML文件。在这个文件中,可以限定要查找的List、ContentType、Column,过滤排出掉不需要使用的。
同时在这个XML 文件中可以给特殊的List名称指定别名。例:
<Web AccessModifier="Public" Class="EpasContext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal">
<List Name="Links" Member="LinksItem">
<ContentType Name="Link" Class="LinksItem">
<Column Name="URLwMenu2" Member="URL3" />
</ContentType>
</List>
<List Name="FormList" Member="FormListItem">
<ContentType Name="项目" Class="FormItem"/>
</List>
<ExcludeOtherLists />
<ContentType Name="项目" Class="Item">
<Column Name="Created" Member="Created" />
<Column Name="Modified" Member="Modified" />
<Column Name="ContentType" Member="ContentType" />
<Column Name="Title" Member="Title" />
<Column Name="Created" Member="Created" />
<Column Name="Author" Member="Author" />
<ExcludeOtherColumns />
</ContentType>
<ContentType Name="文档" Class="Document">
<Column Name="Created_x0020_By" Member="DocumentCreatedBy" />
<Column Name="Modified_x0020_By" Member="DocumentModifiedBy" />
<Column Name="FileLeafRef" Member="Name" />
<ExcludeOtherColumns />
</ContentType>
<ContentType Name="链接" Class="Link">
<Column Name="Comments" Member="Comments" />
<Column Name="URL" Member="URL" />
<Column Name="URLwMenu" Member="URL1" />
<Column Name="URLNoMenu" Member="URL2" />
<ExcludeOtherColumns />
</ContentType>
<ExcludeOtherContentTypes />
</Web>
This section explains how you can change some of the default behavior of SPMetal by using the markup in a parameters XML file.
The top level element is <Web>. Set its Class attribute if you do not want to use the name that SPMetal would give by default to the DataContext-derived class that represents all the website’s content. Set its AccessModifier attribute to “Internal” if you want the class that is generated to be internal (Friend in Microsoft Visual Basic). The default is Public.
If you want to change how SPMetal generates a property to represent a list, add a <List> element as a child to the <Web> element. You can also add a <List> element to force SPMetal to generate a class for a list that it would not model by default, such as a hidden list. Set its mandatory Name attribute to the name of the list.
If you do not want to use the name that SPMetal would give by default to the property that represents the list, use the Member attribute to specify an alternative name. For example, by default, SPMetal uses “Calendar” as the property name for a Calendar list. You can change this to ScheduledEvents by using the markup <List Name="Calendar" Member="ScheduledEvents"/>.
If you do not want the type of items in the list to be what SPMetal would designate by default, set the Type parameter to a different type. For example, you might create (in a separate code file) a class that is just like the class that SPMetal would create as the list item type, except that it has one extra writable property. You could designate your class to be the list item type and have calling code that writes to that property of the list items that are returned by a LINQ query.
Note |
---|
Do not use the Type attribute of <List> to simply rename the class that SPMetal generates for the list item type. Instead, use the Class attribute of a <ContentType> element that is a child of the <List> element. See ContentType Element later in this topic. |
There are two possible child elements to the <List> element: <ContentType> and <ExcludeContentType>. These function identically to the elements of the same names that are children of the <Web> element, except that they apply to content types that are scoped to a list instead of the whole site.
Add this child element to the <Web> element to block SPMetal from generating a property for a particular list. Set the required Name attribute to the name of the list.
Add this child element to the <Web> element to block SPMetal from generating properties for any lists that are not explicitly represented by <List> elements. You cannot have this element and an <IncludeHiddenLists> element.
Add this child element to the <Web> element to force SPMetal to generate properties for all the hidden lists. You cannot have this element and an <ExcludeOtherLists> element.
If you want to change how SPMetal generates a class to represent a content type, add a <ContentType> element as a child to the <Web> element (or <List> element). You can also add a <ContentType> element to force SPMetal to generate a class for a content type that it would not model by default, such as a hidden content type. Set the mandatory Name attribute of the <ContentType> element to the name of the content type. (Use “Item” as the name of the basic SharePoint Foundation content type.)
If you do not want to use the name that SPMetal would give by default to the class that represents the content type, use the Class attribute to specify an alternative name. For example, if the website has a Team Members list and columns were added to it, by default, SPMetal creates a TeamMembersItem class to be the type for the list items, and it specifies this as the type parameter for the EntityList(Of TEntity) property that represents the list. You can change the type parameter to something more readable, such as TeamMember, by using the following markup:
Set the AccessModifier attribute to “Internal” if you want the class that is generated to be internal (Friend in Visual Basic). The default is Public. There are no other options.
Add this child element to the <ContentType> element to force SPMetal to create a property that represents a field (column) that it would not model by default, such as a hidden field. Set the required Name attribute to the internal name of the field. If the parent <ContentType> element is a child of a <List> element, you can also have <Column> elements that represent fields that are in the list, but not in the content type itself, which can occur if a column has been added to a list.
If you do not want to use the name that SPMetal would give by default to the property that represents the field, use the Member attribute to specify an alternative name.
If the field is a choice field and you want the type of the property that models the field to be String even if the field does not allow ”Fill-in" choices, set the Type attribute to “String”. (By default, SPMetal generates a declaration of an enum class using the allowed values specified in the column definition for any choice column that does not allow "Fill-in" choices.) For more information about how LINQ to SharePoint maps SharePoint Foundation field types onto Microsoft .NET Framework 3.5 data types, see Type Mapping: From LINQ to SharePoint Provider to .NET.
Add this child element to the <ContentType> element to block SPMetal from generating a property for a particular field (column). Set the required Name attribute to the internal name of the field.
Add this child element to the <ContentType> element to block SPMetal from generating properties for any columns that are not explicitly represented by <Column> elements. You cannot have this element and an <IncludeHiddenColumns> element.
Add this child element to the <ContentType> element to force SPMetal to generate properties for all the hidden fields of the content type. You cannot have this element and an <ExcludeOtherColumns> element.
Add this child element to the <Web> element (or <List> element) to block SPMetal from generating a class for a particular content type. Set the required Name attribute to the name of the content type.
Note |
---|
You cannot block class generation for a content type if other content type classes are being generated that will inherit from the blocked class. For example, if you tried to block class generation for the basic unnamed SharePoint Foundation content type (which is called "Item") with a <ExcludeContentType Name="Item" /> element, you would also have to add <ExcludeContentType /> elements for every content class that inherits from Item. |
Add this child element to the <Web> element to block SPMetal from generating classes for any content types that are not explicitly represented by <ContentType> elements. You cannot have this element and an <IncludeHiddenContentTypes> element.
Add this child element to the <Web> element to force SPMetal to generate classes for all the hidden content types. You cannot have this element and an <ExcludeOtherContentTypes> element.
<?xml version="1.0" encoding="utf-8"?> <Web AccessModifier="Internal" xmlns="http://schemas.microsoft.com/SharePoint/2009/spmetal"> <ContentType Name="Contact" class="Contact"> <Column Name="ContId" Member="ContactId" /> <Column Name="ContactName" Member="ContactName1" /> <Column Name="Category" Member="Cat" Type="String"/> <ExcludeColumn Name="HomeTelephone" /> </ContentType> <ExcludeContentType Name="Order"/> <List Name="Team Members" Type="TeamMember"> <ContentType Name="Item" class="TeamMember" /> </List> </Web>