“Read-Only” Entity Framework? I'm trying to use RIA Services, EF, and Silverlight

和自甴很熟 提交于 2020-01-14 04:14:18

问题


I'm trying to present MySQL server data in a silverlight client and my current plan of attack is to use Entity Framework to MySQL with RIA Services providing the data access between the Silverlight Client and Entity Framework.

However, I'm only trying to present data to the user and I do not want the possibility of me or someone else being able to change the data in MySQL.

In short, I wish there was a way to simply ignore the setters for all entity types. I only want getters. I want "read-only" access to MySQL. However, it seems my only option is to change "Setter" on each individual field to either Internal, Private, Protected, or Public.

Is there a better way to accomplish what I'm trying to do?


回答1:


If you're using RIA Services...

When you create a Domain Service Class (which is in essence what RIA is), it will bring up a dialog that you can click "enable editing" on. In your case, just don't click it and none of the editing functionality will be generated.

RIA Services is for connecting the server and the client, and it can just Not generate the CUD part of teh CRUD


(source: silverlightshow.net)




回答2:


With the caveat that I've never used RIA services, from what I understand it's built on top of ADO.NET Data Services. There's no way (as far as I know) to stop the creation of property setters in the Silverlight generated proxy, but you can lock down the data service itself with a bit of code in its static InitializeService() method:

public class MyDataService : DataService<MyEntityModel>
{
    public static void InitializeService(IDataServiceConfiguration config)
    {
        // make all entity sets read-only:
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);

        // make the (example) CommentEntitySet read/writeable:
        config.SetEntitySetAccessRule("CommentEntitySet", EntitySetRights.All);
    }
}

With this method, the data service will disallow changes to any entity set except CommentEntitySet (which I included to illustrate the way in which you can can override the access rule to individual entity sets after the first line, which sets the default access rule for all entity sets to AllRead).



来源:https://stackoverflow.com/questions/1493196/read-only-entity-framework-im-trying-to-use-ria-services-ef-and-silverligh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!