Entity Framework generated classes are not in the namespace I require, is there a way of changing the namespace it uses without regenerating?

asp.net-mvc-4 entity-framework namespaces visual-studio

Question

If I need to regenerate where should the namespace be specified. I am trying to use partial classes from within the Models namespace however they don't match.

The simplified code fragment below is where the entity framework classes have been generated

namespace projectname
{
   #region Contexts

   /// <summary>
  /// No Metadata Documentation available.
  /// </summary>
  public partial class MyClass
  {

  }
}

When I add a class to my models folder,

namespace projectname.Models
{

    public partial class MyClass
    {
     //etc, etc 
    }
}

As you can see the namespaces don't match causing issues when I try and use them as the compiler is seeing both projectname.Models.MyClass and projectname.MyClass.

I would like some advice on the correct way to fix this, preferably to update the E.F. classes so they exist in the projectname.Models namespace, but I am not sure how to go about it.

1
16
3/26/2015 1:23:41 PM

Accepted Answer

IIRC, if you are using an Entity data model, you should be able to change the namespace directly through the xml file or files (you may have to change it for all the various storage and and conceptual models). There might even be some way to change it in the designer.. seems simple enough.

Actually, the solution might be here:

Generated Code Overview (Entity Data Model Designer) (see Custom Tool Namespace)

6
12/28/2012 4:47:45 AM

Popular Answer

In line with Roman O's comment, namespace can be changed by updating "Custom Tool Namespace" property of text transform (.tt) file, which ties generated entity classes (in Database First approach) to entity model (.edmx) file. This works in VS 2012 Express with EF 5.0.

I would post a screenshot but for lack of reputation.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow