Ado.Net Entity FrameworkのコードのみとAdo.Net Data Service
-
05-07-2019 - |
質問
Ado.Netのプレリリースを使用していますが、Ado.Net Data Serviceでそれを使用する方法を理解できません。
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 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;
}
}
問題は、Ado.Net Data Serviceがパラメーターなしのコンストラクターを必要とすることです。 そして、コンストラクターを提供する場合、基本コンストラクターに何を書きますか?
ベースコンストラクターを指定しても、コンテキストはBuildContextなしでは完了しません
私が見逃した、またはEntity Frameworkではない「コードのみ」このプレリリースではAdo.Net Data Serviceでサポートされていませんか?
解決
dataserviceクラスのprotectedメソッド:CreateDataSource()をオーバーライドし、ObjectContextのインスタンスを返すことができます。これにより、基礎となるプロバイダーコンストラクター(この場合はEF)が多数のコンストラクターを取るシナリオが可能になります。
これがお役に立てば幸いです。
ありがとう Pratik
所属していません StackOverflow