2

In Visual Studio 2012 Pro or Visual Studio 2015 community edition, is there a way to assign a namespace to an entity data model in order to integrate several models for different database in the same project?

I know it is possible if I create a "Project" but if I would like to create a web site??

please note that "Web Site" != "Web Project"

The edmx files go in the App_Code folder and in VS2012 the namespace property seems ignored, because if I add a second edmx, mapped to another DB, it goes in conflict if there are tables with the same name of the first one.

In VS2015, if you create a web site, there is not even a way to set the namespace property.

Please note that I'm talking about "Web Site" and not "Web Project". They are different.

leppie
  • 115,091
  • 17
  • 196
  • 297
Giox
  • 4,785
  • 8
  • 38
  • 81
  • I tried adding two `edmx` with same name tables, seems to work. What is the error you get exactly. – wonderbell Mar 11 '16 at 20:15
  • @wonderbell I have put the the edmx files inside a subfolder of App_Code closed and restarted Visual Studio, and it has added the namespace. So now the two EDMX are isolated and I get anymore errors. The error was that there were class properties defined twice (Like User.Name, having two table User from different DB, without a namespace, the property Name was defined in two partial classes User) – Giox Mar 11 '16 at 20:36

2 Answers2

0

I have put the the edmx files inside a subfolder of App_Code closed and restarted Visual Studio, and it has added the namespace. So now the two EDMX are isolated and I get anymore errors.

So if you have three different Databases DB1, DB2 and DB3, you can create three different folders in the App_Code folder: App_Code\Db1Model App_Code\Db2Model App_Code\Db3Model

Then inside Db1Model folder create a new ADO.NET Entity Data Model for DB1. In the wizard window when you have to "Select your database objects and settings", you can insert also the "Model Namespace".

Repeat the above tasks for all the databases, using different namespace and putting the model in a new folder.

Try to build the Web Site, if you get the error

"The type Db1.TableName already contains a definition for SomeDbField"

Close Visual Studio and reload the web site.

Tested with Visual Studio 2012, Visual Studio 2015 community edition and EF6.

Why I need to have multiple Data Model?

  1. If you need to build a aggregated reporting web site reading from different databases of other existing applications.
  2. Data migration project
  3. Many other reasons...

Why I want to use New Web Site and not the New Project?

The main reason is that "Web Site" is faster in developing/debugging, as you don't have to recompile on every little change you do in the code-behind. Deploying it's easier as I have to upload only the changed file. I can do quick and simple changes also without Visual Studio. More flexibility, more risk of messing up everything. I know, but depends on how we are used to work and projects needs. For more info read this: Difference between 'Web Site' and 'Project' in Visual Studio

Community
  • 1
  • 1
Giox
  • 4,785
  • 8
  • 38
  • 81
0

I would try to change model class name after generated from table to avoid name clashing, as indicated here, or more precisely here (Changing an entity name).

Using the same data models (POCOs) class names may lead to type load errors, since EF seems to ignore namespace when loading model types as indicated here (basically, the most safe approach is to ensure that entity class name is unique across the application domain).

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • I wuold discard changing entity name one by one, too much work, and if I have to update the model, DB to code, I have to do it again, naaah. Modifing the T4 template could be an option, but I prefer not to touch it, I don't feel it so safe for myself. By the way, I have been able to find a solution, and the namespace are correctly generated and used by EF, see my auto-reply. – Giox Mar 13 '16 at 08:12