having edmx file necesary to convert to entity classes - c#

I have a edmx generated with EF 5 file and I need to convert to entity classes or maybe to dbcontext class. I have the entities with the name class and properties but without the lengths or keys or relationship model.
is it possible to convert to strong entity class?

Related

Entity Framework 6.0 SQL-first synch with database without .edmx?

I have a database with some tables for school related project and I have a model with EF 6.0 SQL-first approach. I need to update the database with a new table & update an existing table with a new column. The twist is: I don’t have any *.edmx file.
How can I update the model without it? If it is impossible, then how can I generate *.edmx without interrupting the existing model?
Entities are essentially POCOs, so you really just need to update your schema and update the entity classes to match. For new entities if the project is not using an edmx then it should either be using classes extending EntityTypeConfiguration or setting things up with the modelBuilder on the OnModelCreating event in the DbContext.
EF can resolve most general mappings using convention, so adding a column to a table usually just means adding the property to the entity. Mapping only comes into play when you want to change a columns naming, handle type casting differences, or use identity/computed columns. For new entities it can also use convention, but commonly there would be config used for the Table name, PK name, and things like Identity columns, plus navigation properties for related entities.

Entity Framework table per concrete type issue

I'm using Entity Framework 7 with TPC (table per concrete type) inheritance and trying to figure out the following scenario:
I have an entity called Ad. This class is base and abstract. The derived classes from Ad are: CarAd, MotorAd, etc... Using TPC I'm able to create separate tables from my derived entity classes and having EF not creating a table for my base class (which is correct behaviour). So far so good. I also have an entity called Setting which has foreign key to my base Ad entity.
This relationship between Ad and Setting breaks my table-per-concrete type structure and EF creates a table only for Ad entity by combining all properties from the derived classes and therefore there are many nullable columns - and TPH (table per hierarchy) inheritance occurs - not my desired result. How can I change my structure to fix this problem?

EF Poco Generate for specific table

It is possible to generate a POCO class for one selected table in entity framework.
There are few tools that does that, but it does generate for all the table,
The primary reason i am asking this is to generate a POCO class for a single table, and add attributes to it manually, and the all i need to do is to attach it to DbContext as a property :).
As for example using Package-manager, they way it is done to add migration
generate-poco TableName FileName.cs
You can use the ADO.Net wizard to generate the POCO classes for you from the database. The wizard will generate one partial class per table with properties that match your field names. Here is what I've done in the past when I wanted to add a new large table to an existing code first context.
Add->New Item -> ADO.Net Entity Data Model
Move the generated POCO classes to some other folder (and edit the namespace)
Delete the rest of the extra stuff (I.e the .edmx file and generated context)
Add the DBSet to 'your' DBContext
If you are using EF migrations
add-migration
update-database
There are a lot of extensions for visual studio for that.
EF 4.x POCO Entity Generator for C# Free (try this one and you only need to map the table)

How to Convert a normal class.cs to DataModel.dbml

I have 20 class.cs with x00s properties for my old project
and now i want to use EntityFramework
but I dont want to rewrite all this classes from begin
i just want to convert this class to DataModel.dbml or use some thing like add Existing Item
and I tried creat class with same name and copy the code inside them but the editor clear them after saving
how can i do that ?
There is plenty of resources regarding Plain Old CLR Objects and Entity Framework code first model on the web.
ADO.NET EF 4.0: Working with Plain Old CLR Objects (POCO) Classes
Code First step by step tutorial
But the gist of it is:
Entity Framework >4.0 supports POCO types that don’t need to inherit from a base class or
implement any interfaces to get persistence. There is also no need for metadata or mapping
attributes on type members, so you can use your existing code as simple entity classes.
An EDMX file that contains the conceptual model is still required. Add a new ADO.NET Entity Data Model to the project.
Using the toolbox, drag entities and associations from the toolbox and design the conceptual model. Simply make sure that the names on your POCO classes match the names of your conceptual entities.
Add entity keys as you would add primary keys in a db. Add associations like foreign keys in a db.
In Solution Explorer, click the EDMX file and then, in the Properties window, clear the Custom Tool property to turn off the automatic generation of .NET classes for your conceptual model.
Right click edmx design area and select 'Generate database from model'. DDL will be produced. Run that to create your db.
Make sure your POCO classes and POCO edmx model are in a separate assembly.
Create your custom ObjectContext derived data context with ObjectSet<T> members like here. Newer EF releases use System.Data.Entity.DbContext and System.Data.Entity.DbSet<T> instead.
You can use dbml too (Linq To Sql classes instead of EF), but you still need to generate your model from scratch, like you'd do with EF.

How to convert my EF Code-First to Database first?

I want to convert my code-first project to Database first. Is there an automated way or should I just delete the entities and context code and create a model from the created database?
It appears Entity Framework Power Tools supports a feature called "Reverse Engineer Code-First". You can download it here.
There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes.
However, you might not want to delete your code-first classes right away. The entity framework database-first model creates partial classes for all of the entity objects. If you have any business logic (anything besides the plain old properties) in your code-first classes, you can declare them as partial, remove the properties, and maintain the business logic. Essentially, you're letting EF generate the properties in the *Designer.cs file, while you define the business logic in your .cs files.

Categories