WCF数据服务和实体框架代理对象

时间:2011-06-07 13:36:15

标签: wcf entity-framework-4.1 wcf-data-services ef4-code-only

我对 WCF DataService Entity Framework 4.1(代码优先)有疑问。 所以我在Web服务器上有一个DataService:

 [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CrmDataService : DataService<CrmDataContext>
    {
        private static CrmDataContext _mdc;
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.UseVerboseErrors = true;
        }

        protected override CrmDataContext CreateDataSource()
        {
            _mdc = new CrmDataContext(@"Data Source=localhost;Database=MyDb;User Id=sqluser;Password=111111;") { TablePrefix = "crm_" };
            _mdc.Configuration.ProxyCreationEnabled = false;
            return _mdc;
        }

我还有一个CrmDataContext使用的实体对象列表(例如公司,地址,人等) 将此服务添加到我的客户端应用程序(例如,服务命名空间)后,我获得了相同的实体对象,但是在服务名称空间中。当然,我希望通过数据服务获取任何公司对象(例如),它从命名空间服务返回一组实体对象。

所以我的问题是如何告诉数据服务使用我的真实实体对象,而不是在我的项目中创建这些其他代理对象? 如果无法做到,那么如何将从数据服务获得的对象复制到我的真实实体中呢?

我的目标是通过使用数据上下文的数据服务从服务器获取一些实体对象,而不是客户端上的实体对象。我想为本地和服务器端的所有实体对象使用一个程序集。

1 个答案:

答案 0 :(得分:3)

如果要使用相同的对象,则无需将服务添加到客户端应用程序中。只需将包含类型的程序集添加到引用的程序集中,然后在客户端应用程序中,使用服务uri创建DataServiceContext。

你必须做这样的事情:

context.CreateQuery(entitysetName)。

T是您在服务和客户端中使用的常见类型。

要记住的一件事是,如果实体中的键不符合约定,则可能必须在类型上添加DataServiceKeyAttribute或DataServiceEntityAttribute。

希望这有帮助。

由于 PRATIK