1

I have my data model in project A, in the namespace ProjectA.Model.MyEntities . In project B (an MVC2 project), I reference project A. But when my pages are requested, they are choking saying that 'type or namespace ProjectA is not known.

When I compile my project, I get no errors. From my controller, the database context seems to be working fine, as references to MyEntities compile fine. Going to definition on MyEntities navigates me to the Model.Designer.cs file in project A.

It's like project A is reference during static compile, but not at run time.

This is an MVC2 project, running on .Net4, in Visual Studio 2010, using ASPX engine. It is a legacy project, that is why it is MVC2, but it has been upgraded to .Net4.

What do I have to do to get rid of this error. Have been beating my head on the wall over it all morning.

1 Answer 1

2

You could try adding the namespace for ProjectA.Model.MyEntities to the web.config, this will inform the application that the namespace is required for all views.

<system.web>
    <pages>
        <namespaces>
            <!-- defaults omitted for clarity-->
            <add namespace="ProjectA.Model.MyEntities" />
        </namespaces>
    </pages>
</system.web>

Alternatively, you can use an imports directive at the top of the individual views - this would be more appropriate if you are only using ProjectA's entities within a limited number of views:

<%@ Import Namespace="ProjectA.Model.MyEntities"%>

This should be included after the @Page or @Control directive.

Sign up to request clarification or add additional context in comments.

4 Comments

I get the same error if I do these, the only difference is that the error indicates that the namespace is now in the Web.config. It's like it isn't loading the assembly which has the data model in it.
Have you made sure the Reference to ProjectA has CopyLocal set to true? (to check this right click on the reference on ProjectB, properties, Copy Local). If this is not set to True then the compiled dll from ProjectA will not be copied to the output directory, so the application will not be able to use it at runtime.
I've tested this locally and it works fine. The only thing I can guess at is that there's possibly a naming conflict between your entities and a system type/method. If you could post more information (e.g. the specific error, the class definition of the entity which fails and the code being used to call it in the view) then I / others might be able to provide more help.
I trashed the project and started over copying over my source files. You are correct it should have worked AFAIK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.