使用 Ado.Net 数据服务的 Ado.Net 实体框架纯代码
我使用的是 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 数据服务不支持实体框架“仅代码”?
Im using the pre release of Ado.Net and can't understand how I use that with Ado.Net Data Service.
The code for the 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"));
}
And the code for Ado.Net Data Service
[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;
}
}
The problem is that Ado.Net Data Service expect a constructor with no parameters.
And if I provide a constructor what will I write to the base constructor?
And even if I specify the base constructor the context isn't complete without BuildContext
What have I missed or isn't Entity Framework "code only" not supported with Ado.Net Data Service in this pre release?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以重写数据服务类上的受保护方法:CreateDataSource(),并且可以返回 ObjectContext 的实例。这使得底层提供者构造函数(在本例中为 EF)采用一堆构造函数的情况成为可能。
希望这有帮助。
谢谢
普拉蒂克
You can override the protected method: CreateDataSource() on your dataservice class, and can return the instance of the ObjectContext. This enables the scenario where the underlying provider constructor(in this case EF) takes a bunch of constructors.
Hope this helps.
Thanks
Pratik