Same table names, different schema issue in EDMX (Database first) - c#

I have two tables with Same table name and different schema in the same database.
For example - Vendor.Employee and Customer.Employee
I am now adding EDMX file in my Class Library project under VS 2012. I am using EntityFramework 5.0.
Though it creates edmx files successfully without any issues, but it creates these entities with different names (like, Vendor.Employee is created as Employee entity, whereas Customer.Employee is created as Employee1 entity.
I understand its because both the classes are directly under same namespace, and so they get created with same names.
But is there any way (for example, giving namespaces per schema) or alternate to resolve this issue so we can have same table names.
Any help on this much appreciated.
Thanks and Regards

Related

Entity Framework create tables or fields if using dbfirst

The project uses dbfirst approach. On my database I create and edit tables , Then I update the models from the database (edmx).
Is there a way to solve the problem when my models are more relevant than the customer base? It is very important to solve this with the help of EF.
That is, I need to in the customer database automatically added new fields in the tables and created the tables themselves if they were not
You can use Visual Studio over your database model through the designer interface and them updated your database. However, it's a procedure i don't recomend you to do, because depending what operations you want to perform on your database after being applied can rise errors related with some specific types and other specific things and then the roolback could not be so easy.

Data First EF, System Table clashes with System Namespace

I'm developing a .NET project and am incorporating a data-first link with a database. Unfortunately, the database has a mandatory table called "System", and the generated class in the model is clashing with the System namespace, overriding it. I have tried commenting out the class and all references to it, but then the Context throws missing relationship errors. Any ideas on resolving this conflict?
I am new to data-first EF, so I'm sure there must be some work around I'm not familiar with. Perhaps renaming the class and all its references within the class, but mapping it to the correct table in the context/edmx? I'm not sure how to do that though. I'm using EF 6.2.
You can change the name in the EDMX designer. That will rename the class and keep it mapped to the System table. – Gert Arnold
This was the correct answer. Intellisense didn't volunteer to fix the class names for me after renaming in the EDMX, so I had to spend 15 minutes updating the class, its references, and the 20ish times it was referenced in the EDMX file itself.

Entity framework "default schema" in SQL Server?

We have a complete application using Entity Framework. There are 30 tables with many interconnected relationships. All of these tables exist in an SQL Server schema associated directly with this application.
(By "schema" I mean a SQL "namespace", not "schema" as in "the definition of my tables").
For example, this schema is created as:
CREATE SCHEMA [myApp]
Additionally, we have a "master" schema that has data that is common across a few applications. (We use schemas to separate applications within a single database catalog.)
Now, I've been asked to bring up a second instance of our application with its own independent tables. So for example:
CREATE SCHEMA [mySecondApp]
This new schema is to contain all of the same tables and relationships between them as is in the [myApp] schema. It will still be referencing our "master" schema as well.
The problem is it looks like Entity Framework directly references the table by fully qualified name, which includes the schema. (e.g. MyDB.MyApp.table1 and so on). This means that in order to support this scenario I either need to convince IT to give me another catalog, which will need its own real-time-synchronized copy of the master schema data, or I need to get Entity Framework to reference another schema.
Short of literally find/replacing the schema across the entire EF model, then separately maintaining both models (destroying all the benefits of e.g. migrations), how can I do this? Ideally I'd like a variable in which I can hold the schema, which can be set in the Web.config file, which EF will then pick up on and use everywhere, including in migrations and the application itself.
The pipe dream: Change a value in web.config, run Update-Database, and have a ready-to-go schema with all the tables pre-populated as per our migrations and Seed method. Change the value again in web.config and do it again as many times as desired with different schema names. (Note that the master schema references should remain intact - this is why I'm thinking about some kind of variable.)
Of course also, change the value in web.config on the IIS application server and point the app at the other schema. Then I simply setup multiple deploy profiles with Web.config transforms for each instance on our IIS.

System.Data.SQLite.EF6 Entities not found after modifying edmx diagram

I'm using EF6 with System.Data.SQLite 3rd party provider (there is no native support for SQLite in EF6).
I have to add inheritance between two tables. Because I can't do this on database level, I have to modify the .edmx diagram:
remove the original connection between the two tables (1:N connection), and replace it with the inharitance
rename one of the two tables, because EF automatically generates the name, which is not fit to our naming conventions
After I do this Visual Studio won't see the tables (none of them, nor the tables I have not modified), and I get several errors after build ("The type or namespace '' could not be found." ). Also, all of my table.cs files get removed from under ApplicationDatabase.tt.
If I undo the changes (revert back to the 1:N connection), and save the diagram and the database model everything works fine again.
What am I missing during the changes?
Ok, I solved it by removing the Id from the inherited table. I think the problem was that both tables had an Id, and it conflicted somehow because of the inheritance. Now everything just works fine.

Entity Framework conflict with same table name from different databases

I am using Entity Framework 4 with MVC 3 in Visual Studio 2012 (C#).
I am using database first; there are two separate databases each with its own namespace and with two separate edmx files. Each database has a table with the same name and fields (but different content). When I added the second table I started to get compile errors.
Ambiguity between 'Interface.CodeFormStatus.FormStatusCodeID'
and 'Interface.CodeFormStatus.FormStatusCodeID'
There seem to be some complex workarounds or I could rename one of the tables. Is there not a straightforward solution as this must be a fairly common issue.
I ran into a situation where I had two databases (one an older version of the other) and I needed to integrate both into a single project. Naturally, almost every name conflicted.
I created two separated edmx files for each database, and put each in its own namespace for clarity. I then edited each entity name to reflect which database it was coming from - (e.g. "Activities", which was in both, became "v13Activities" and "v14Activities").
For operations which were to be mirrored between both databases, I wrote a wrapper that included both contexts. This made my code was much less repetitive, and it had less synchronization issues.
Hope this approach helps someone else - it seems like this is an obscure question, and this answer was one of the top results on Google!
Update: In EF 6.1+, there is another solution. You can have "conflicting" names, and separate them with simple namespacing when using the "Code First From Database" option. I would advocate for this solution going forward, as the old XML .edmx style is going to be phased out starting in EF Core.
This worked for me. Just click on the table in the designer (the graphical version not the code) Then in the properties next to the, "Name" attribute you can change the name to something different. This will just change the name within the designer and used more as an alias throughout the application.
If you don't have many tables with the same name, then you could edit entity name in designer (your .edmx file).
So, just double-click a name of one of your CodeFormStatus entities and make it different (for example, change it to CodeFormStatusOther)

Categories

Resources