我正在使用Ado.Net的预发行版,无法理解我如何在Ado.Net数据服务中使用它。

ObjectContext的代码

 public class TradingContext : ObjectContext
    {
        private static TradingContext _Context;

        public static TradingContext Current
        {
            get 
            {
                if (_Context == null)
                {
                    _Context = BuildContext();
                }
                return _Context;
            }    
        }

        public TradingContext(EntityConnection conn) : base(conn)
        {

        }

        public IObjectSet<Message> Messages
        {
            get { return CreateObjectSet<Message>(); }
        }

        private static TradingContext BuildContext()
        {
            var builder = new ContextBuilder<TradingContext>();
            builder.Entity<Message>().Property(x => x.MessageId).IsIdentity();
            builder.Entity<Message>().Property(x => x.Xml).HasStoreType("xml");

            return builder.Create(new SqlConnection(@"connection string information"));
        }

Ado.Net数据服务的代码

 [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]   
    public class Trading : DataService<TradingContext>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }

问题是Ado.Net数据服务期望一个没有参数的构造函数。 如果我提供构造函数,我将写入基础构造函数?

即使我指定了基本构造函数,如果没有BuildContext

,上下文也不完整

我错过了什么或不是实体框架“仅限代码”此预发行版中不支持Ado.Net数据服务吗?

有帮助吗?

解决方案

您可以在dataservice类上覆盖受保护的方法:CreateDataSource(),并可以返回ObjectContext的实例。这使得底层提供者构造函数(在本例中为EF)占用一堆构造函数的场景成为可能。

希望这有帮助。

由于 PRATIK

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top