当前位置: 动力学知识库 > 问答 > 编程问答 >

wcf - Breeze Metadata Only EFContextProvider Exception

问题描述:

After evaluating the various strategies for generating Metadata it seems the Edmunds model suites are situation as the Wcf Client serves as our model and it is something we don't have total control over.

I wrote a unit test to encapsulate the functionality to a simple interface called IMetadataProvider that simplely has a get only string property to return the metadata from EFContextProvider.

Largely the examples will show the DbContext injected into the WebApi controller(s) and for our implementation that will not work. Out Web-Api-2 controllers are hosted in a separate project from the main website and additionaly have a Uow/Repository implementation provided by another dev team. Enter the abstraction.

What have I overlooked?

/// Implementation of Common Contract Interface

namespace SMB.Client.Metadata

{

#region Usings

using Breeze.ContextProvider.EF6;

using SMB.Core.Contracts;

#endregion

public class ClientMetadataProvider : IMetadataProvider

{

public string Metadata

{

get

{

EFContextProvider<ClientMetadataDbContext> breezeContextProvider =

new EFContextProvider<ClientMetadataDbContext>();

return breezeContextProvider.Metadata();

}

}

}

}

/// Unit Test

namespace SMB.Client.Metadata.Tests

{

#region Usings

using Microsoft.VisualStudio.TestTools.UnitTesting;

using SMB.Core.Contracts;

#endregion

[TestClass]

public class ClientMetadataProviderTests

{

IMetadataProvider TheMetadataProvider;

[TestInitialize]

public void Initialize()

{

this.TheMetadataProvider =

new ClientMetadataProvider();

}

[TestMethod]

public void ClientMetadataProviderTests_MetadataPropertyIsNotEmpty()

{

string theActualMetadata =

this.TheMetadataProvider

.Metadata;

Assert.IsFalse(string.IsNullOrEmpty(theActualMetadata));

}

}

}

I get the following exception

Test Name: ClientMetadataProviderTests_MetadataPropertyIsNotEmpty

Test FullName: SMB.Client.Metadata.Tests.ClientMetadataProviderTests.ClientMetadataProviderTests_MetadataPropertyIsNotEmpty

Test Source: c:\all\tfs\Main\Source\SMB\Tests\SMB.Client.Metadata.Tests\ClientMetadataProviderTests.cs : line 24

Test Outcome: Failed

Test Duration: 0:01:06.5845416

Result Message:

Test method SMB.Client.Metadata.Tests.ClientMetadataProviderTests.ClientMetadataProviderTests_MetadataPropertyIsNotEmpty threw exception:

System.NullReferenceException: Object reference not set to an instance of an object.

Result StackTrace:

at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)

at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()

at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()

at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)

at Breeze.ContextProvider.EF6.EFContextProvider`1.GetMetadataFromDbContext(Object context)

at Breeze.ContextProvider.EF6.EFContextProvider`1.GetMetadataFromContext(Object context)

at Breeze.ContextProvider.EF6.EFContextProvider`1.BuildJsonMetadata()

at Breeze.ContextProvider.ContextProvider.Metadata()

at SMB.Client.Metadata.ClientMetadataProvider.get_Metadata() in c:\all\tfs\Main\Source\SMB\Client\SMB.Client.Metadata\ClientMetadataProvider.cs:line 19

at SMB.Client.Metadata.Tests.ClientMetadataProviderTests.ClientMetadataProviderTests_MetadataPropertyIsNotEmpty() in c:\all\tfs\Main\Source\SMB\Tests\SMB.Client.Metadata.Tests\ClientMetadataProviderTests.cs:line 25

/// Xml App.config [Copied from DocCode sample]

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<configSections>

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

</configSections>

<entityFramework>

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

<providers>

<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

</providers>

</entityFramework>

<runtime>

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<dependentAssembly>

<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />

<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />

</dependentAssembly>

</assemblyBinding>

</runtime>

</configuration>

网友答案:

After experimenting I decided to see if the underlying connection to the datastore was the issue, which it was. The error is basically stating that it cannot reach the storage mechanism (whether or not the DbContext will actually do anything with the data store is a separate matter.)

The modified App.Config (Yes no Db was ever created).

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="ClientMetadataDbContext" connectionString="Data Source=.\;Initial Catalog=ClientMetadata;Integrated Security=true;Persist Security Info=true;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

After digging through the DocCode sample I found this comment in the web.config and I would answer that yes, you will need it.

<!-- TODO: is this needed?
     phony FoosMetadataContext connection enables FoosMetadataProvider to succeed; there is no nonsense.sdf-->
<add name="FoosMetadataContext" connectionString="Data Source=|DataDirectory|nonsense.sdf" providerName="System.Data.SqlServerCe.4.0" />
分享给朋友:
您可能感兴趣的文章:
随机阅读: