当表存在时,实体框架说无效对象名称

时间:2016-06-13 19:32:42

标签: c# .net entity-framework asp.net-web-api

我必须跳进一个复杂的项目,并在WEB API服务中为特定的存储库进行单元测试。数据库CRUD由实体框架处理。

    private class IntegrationScope : AutoRollbackTransactionTestScope<DocumentRepository>
    {
        public IntegrationScope()
        {
            DependencyResolverMock = MockDependencyResolverFor<IResourceUrlBuilder, ResourceUrlBuilder>(new ResourceUrlBuilder());
            LoggingProviderMock = new Mock<ILoggingProvider>();

            // use real document micro service AutoMapper & Unity configuration
            AutoMapperConfig.RegisterMaps(Mapper.Configuration, DependencyResolverMock);
            UnityConfig.RegisterTypes(TestScopeContainer);

            //Set the DomainId
            TestId = Guid.NewGuid();

            TestDocument = BuildDocument(TestId);

            // get the real object
            DocumentStoreDbContext = TestScopeContainer.Resolve<IDocumentDbContext>();

            InstanceUnderTest = new DocumentRepository(DocumentStoreDbContext);
        }

        public static Web.Service.DocumentStore.Domain.Document BuildDocument(Guid documentId)
        {
            // Invoke GetBytes method.
            byte[] array = Encoding.ASCII.GetBytes("Byte Repository Test");

            return Builder<Web.Service.DocumentStore.Domain.Document>
                .CreateNew()
                .With(t => t.Id = documentId)
                .With(t => t.Data = array)
                .Build();
        }

当它到达DocumentStoreDbContext行时,没有编译错误但是我得到一个运行时错误,说明对象名称无效:

enter image description here

在做了一些研究之后,这个错误似乎是因为数据库/表格不存在,但我可以看到它确实存在:

enter image description here

我做错了什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

InnerException将表格名称显示为DocumentStore.Documents,但您的SSMS屏幕截图显示为dbo.Documents

这可能是原因吗?