Breeze Metadata Only EFContextProvider异常

时间:2014-05-19 00:28:36

标签: wcf metadata breeze soa

在评估了生成元数据的各种策略之后,似乎Edmunds模型套件的情况就像Wcf客户端作为我们的模型一样,而且我们无法完全控制它。

我写了一个单元测试来将功能封装到一个名为IMetadataProvider的简单接口,它简单地只有一个get get string属性来从EFContextProvider返回元数据。

很大程度上,这些示例将显示注入WebApi控制器的DbContext以及无法正常工作的实现。 Out-Web-Api-2控制器托管在与主网站不同的项目中,另外还有另一个开发团队提供的Uow / Repository实现。输入抽象。

我忽略了什么?

/// 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));
        }
    }
}

我收到以下异常

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>

1 个答案:

答案 0 :(得分:0)

在试验之后,我决定查看与数据存储区的基础连接是否是问题。该错误基本上表明它无法访问存储机制(DbContext是否实际上会对数据存储执行任何操作是另一回事。)

修改后的App.Config(是的,没有创建Db)。

<?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>

在深入了解DocCode示例后,我在web.config中找到了这条评论,我会回答是的,你需要它

<!-- 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" />