您的位置:首页 > 运维架构

Use Matadata file to add attributes for properties in Ria service

2010-11-05 11:18 316 查看
When you are using Ria service. You will have to create your own ADO.net entity modal. In this case, the designer will generate a lot code for you. But in the generated code, you want to add some useful attributes for some column. such as [Include] attribute, it will allow you to get the navigation property and transfer to your silverlight client. Without this attribute, you will not getting data for this navigation property at your client.

Of courese, you can also add the attribut directly on the property as following, it will work for sure.

/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("MyTradeModel", "RoleUser", "Role")]
[Include]
public Role Role
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Role>("MyTradeModel.RoleUser", "Role").Value;
}
set
{
((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Role>("MyTradeModel.RoleUser", "Role").Value = value;
}
}


But here is a problem, when you want to update your entity modal from database, you will have to use "Update modal from database". after doing this, all your modifications will be removed. you have to readd them once. if there are a lot of code, it is disaster for the project.

Luckily, i found we can create our own matadata class for this situation. Just add the following code in your project, it will work.

[MetadataTypeAttribute(typeof(User.UserMetadata))]
public partial class User
{
internal sealed class UserMetadata
{
[Include]
public Role Role { get; set; }
}
}


And this matadata file won't be overwritten by "Updata modal from database".
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐