EF 5.x DbContext Generator - Namespace Issues - c#

I'm trying to update a project to work in ASP.NET MVC4 using EF5(Database First approach as there is an existing db). I'm doing this with VS2010.
I am following the steps in this example.
These are the steps that I'm taking:
Set up a new MVC4 project simply called Project.NET
Create the Entity Data Model as ProjectModel.edmx
The connection string is saved as ProjectEntities
The model namespace is set to ProjectModel
Add an EF 5.x DbContext Generator item called ProjectModel.tt
So after all that, it has created the classes to be used by EF; the problem is that these classes are all created with the project's root namespace, rather than the namespace I have provided it.
If I open up any of the generated classes, I get a list of errors that are fixed by manually changing the namespace to ProjectModel
I'd appreciate any thoughts.
Update:
This is a screenshot showing the project structure, an example of the generated code, and the compiler errors.
What's odd is that it only seems to throw an error for a namespace based on the structure, so in the image Project.Models as the namespace creates an issue, but if I type in anything else such as ProjectModels or Test the errors disappear. I can change the the namespace on all the files, sure, but every time the model changes and is updated, the namespace will be reset and the errors return.
This question seems to be the same issue as the one I am having, but unfortuntely, the only answer given advised checking references; I believe mine are fine as System.Data.Entity and EntityFramework are present.
Update 2:
If I don't select all tables to during the code generation no namespace issues appear. I'm currently updating the model in 20 table intervals, with 400 tables though, I'd rather figure out what the actual issue is. I'm assuming it is due to the structure or name of one or more tables, but I'm not sure on whether or not there are any specific rules with regard table structure or naming with regard to EF.

Looks like your project is targeting .NET 2.0. Right-click on your project and choose Properties. make sure Target framework is set to .NET 4 or later.

It appears as though one of the tables which is called System was conflicting with the .NET System namespace. I guess I'll just have to change the table name to something else.

If you are adverse to making changes to your data schema, you can configure Entity Framework to map the table name to a different type/class. Something like this may help.

I achieved to set a custom namespace for the generated classes by setting the Custom Tool Namespace field in the .edmx model properties, and also in the properties of both the .tt files under it, using the Properties window.

Related

EF Core : how to re-scaffold without losing custom changes?

There is a WPF application that is currently transitioning from Entity Framework 6 to Entity Framework Core 5 (database provider is Oracle, the application uses .NET 5 so EF Core 6 isn't possible currently).
There is one big issue to solve: two scaffolded data types weren't correct (from bool to byte and from bool to int). I changed those types manually. If I now re-scaffold, those changes will be removed for sure.
So my question: is there any possibility to re-scaffold (using the -force parameter) without losing the manually changed types OR is there any possibility to override the types which will be generated by scaffolding?
In Entity Framework 6, we used custom data type on EDMX creation to solve this issue:
What I tried and missed: creating a partial class with the same class name (but different file name) which only contained my changes.
The reason why I'm not using migrations is that I use different database stages.
Thanks for your help!
What I tried and missed: creating a partial class with the same class name (but different file name) which only contained my changes.
This will work with the caveat that when the db is re-scaffed the new entities will have the same props appear again (but wrongly typed) and if they're also defined in your partial class you'll get a compile error due to two members having the same name
I'd recommend you install EFCore Power Tools extension and use it to scaff (Reverse Engineer) because there's a section of the "wizard" where it asks you which things you want to scaff and you can untick those columns in the DB to omit them from the models. If they take part in some relationship or have atypical properties meaning they get special attention in the fluent config then you might have to consider an alternative strategy that I use a lot; scaff to a different folder and then run a diff tool to help you merge changes into the main model, then delete the new scaff set.
I leave myself comments in the context that "is the main one", such as "//do not remove this enum column config during context merge", having set up some fluent blah that I know will not be present in the new scaffing
EFCPT does also have some abilities to customize the generated code but I don't know if it goes as far as entirely rewriting parts (I've never looked). It's open source so you can also look at modifying it to meet your needs

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.

Differences between TableAttribute class in System.Data.Linq.Mapping and System.ComponentModel.DataAnnotations.Schema

I am using EF 6.1 where I was trying to get a simple sample code running.
I accidentally used the namespace System.Data.Linq.Mapping (VS Intellisense) to realize the TableAttribute name parameter had no effect, the code created a new table (since POCO class name is different from table Name) when was executed. However, it was expected to use and existing table.
Latter after few googling effort I noticed the namespace System.ComponentModel.DataAnnotation is used in some places. When I made this change, my sample started using the existing table rather than creating it.
I would like to know
What is the difference between the TableAttribute class in these two namespaces?
Why did the TableAttribute in System.Data.Linq.Mapping not yield the expected behavior?
System.Data.Linq.Mapping.TableAttribute belongs to LINQ to Entities.
Entity Framework uses the System.ComponentModel.DataAnnotation.TableAttribute

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)

EF 4.1 DbContext Generattor - Put Entities in different project?

As a part of our application architecture, we like to define clear lines between our functional layers. A typical application solution, therefore, will contain:
Entity
Model
Task
Presenter
FrontEnd
These end up being completely distinct assemblies.
The Entity/Model delineation is done to keep database access functionality in a separate layer from our POCOs, so that only Task ever need know about Model, while everyone up to Presenter knows about Entity
This works well when using Code-First or Fluent-API - but due to the lack of support for SPROCs in those paradigms, it turns out that under EF 4.1 I must use EDMX models.
So - I'm generating POCOs using a DbContext Generator, but the resulting classes end up under .Model, and while I can force their namespace into .Entity instead, they still live in the .Model assembly, which means now .Presenter must reference .Model to get to classes that should be in .Entity.
Is there a way to force or trick EF to dump its generated output into a different Project?
Sure. DbContext Generator are just two T4 templates. You can move the template generating entities to other project. You just need to modify template to point to correct EDMX file. This is default:
string inputFile = #"Model.edmx";
You must change it to relative address to your EDMX file. It will be something like:
string inputFile = #"../Model/Model.edmx"
The template will automatically use default namespace of current project for generated entities but you will have to modify the second template for context to use the new namespace so that entity types are correctly resolved from referenced assembly.
There is small disadvantage of using template in another project - it will not update automatically when you modify model. You must always trigger entity recreation manually by using Run custom tool from context menu on template file.

Categories

Resources