Entity Framework destriong my classes - c#

Why when I saving the EDMX file in VS2012, hi always change the attributes of the component classes?
I use a marker there [NotMapped] with System.ComponentModel.DataAnnotations.Schema and it is always the same clause and using are removed.
EF does not support Agility methodologies?
1) I want to just make simple calculations on the data and display them in the attributes. For example, the document number is the number and prefix.
2) the model-first and code-first for me is not enough. I create an application based on data from the ERP and I have to add Me own document type. Half of the data is in the database and I can not duplicate it. The other half is my new tables. At the same time I do not know yet where I will use the data and I am not sure what the relationship between them I used. (I can not create a relationship right away in the diagram on the 500 tables). The client does not know yet what the data which depends. Typical thing to Agile methodologies.

Learn and use the code-first approach. It will give you full control over your POCOs (plain old class objects). The model-first approach requires that you use the EDMX modeler to make your changes which does not allow you to do much customization underneath.
Entity Framework Tutorial website is a good resource to get started as is the official Entity Framework website.

Related

How to generate Entity Framework 6.x POCO classes with mappings from an EDMX file?

I'm in the process of converting an extensive EDMX model into POCO classes. I need to go from a Database First approach (EDMX with ObjectContext) to a pure Model First approach (DbContext with no EDMX file). I need to use the latest Entity Framework stable version: 6.1.1.
I've tested some approaches:
Adding a the EF 6.x DbContext Generator code generation item by right-clicking the blank space in EDMX designer. This works fine, but it doesn't add any mappings. With this approach I have to still use the EDMX file. It's not full Code First.
Using the EF 5.x DbContext Fluent Generator for C#. This triggers an exception in design time. I'm not being able to use it. I don't know if that's because my VS Entity Framework tools are already updated to 6.x. Using the alternative TT in the comments, that suggests that it would work with EF 6.0 also doesn't help.
Using the EntityFramework Reverse POCO Generator. This is the worst because it won't consider any of my classes and navigation properties renames.
Using the Entity Framework Power Tools Beta 4. Again, it only supports generating from the database, not from the EDMX file.
My requirements:
I need the input to be the EDMX file, not the database.
I need the output to be a full Code First approach with Fluent mappings.
I need all my navigation property names defined in the EDMX to be considered because otherwise it would break a large codebase, even more then migrating from ObjectContext to DbContext will break.
What do you think would be a good option for me to go?
Well i don't think there is an easy one click solution to this.
Underneath you edmx files. You have two more files available besides the xx.Designer.cs and xx.edmx.diagram.. called xx.Context.tt and xx.tt where xx is the name of your edmx model.
These are t4 templates which genrate your dbcontext and poco objects. All your poco objects would be created underneath your xx.tt files and dbcontext underneath your xx.Context.tt files.
You now have to moves these into separate files. This is much easier if you are using EF6. and the file generated are already using DbContext and not ObjectContext.
I faced a similar case and I used Entities to DTO's generator.
Although its purpose is to generate DTO's, however, I believe it can help someone in you case.
https://entitiestodtos.codeplex.com/

EntityFramework (ORM) object names and domain model names - how to avoid duplicates?

Let's say I have a project where I use Entity Framework, but I want to use my own classes instead of the EF classes.
Reasons for using my own classes:
Easy to add properties in code
Easy to derive and inherit
Less binding to the database
Now, my database has table names like User and Conference.
However, In my domain project, I also call my files User.cs and Conference.cs.
That means I suddenly have two objects with the same naming, which is usually very annoying to work with, because you have to use namespaces all the time to know the difference.
My question is how to solve this problem?
My ideas:
Prefix all database tables with 'db'. I usually do this, but in this case, I cannot change the database
Prefix or postfix all C# classes with "Poco" or something similar
I just don't like any of my ideas.
How do you usually do this?
It's difficult to tell without more background but it sounds like you are using the Entity Framework designer to generate EF classes. This is known as the "Model First" workflow. Have you considered using the Code First / Code Only workflow? When doing code first you can have POCO classes that have no knowledge of the database, EF, or data annotations. The mapping between the database and your POCOs can be done externally in the the DBContext or in EntityTypeConfiguration classes.
You should be able to achieve your goal of decoupling from EF with just one set of objects via code first.
To extend the above answer, the database table name User (or Users as many DB designers prefer) is the identifier for the persistence store for the object User that's defined in your code file User.cs. None of these identifiers share the same space, so there should be no confusion. Indeed, they are named similarly to create a loose coupling across spaces (data store, code, development environment) so you can maintain sanity and others can read your code.

Suggestions for starting a entity framework code first project from scratch

I'm trying to create a project from scratch. I'll be using asp .net mvc4 (with asp net web api), and entity framework 5 for data access (all the latest technologies)
Since it's a fresh start, I was thinking on centering my design on my model rather than creating the database first and then creating the EF model, so I though I'd go with a code first approach.
The problem with code first (as far as I see) is that you lose all the scaffolding that EF does for you on a model first scenario (design support, easily generating and maintaining entity relationships 1-1, 1-*, -, etc)
The question is : What tools or templates or snippets or whatever can I use to make my life easier when designing my model?. I want this process to be as painless as possible, since it involves a lot of repetition (FK relationships, for example, are the same always)
Should I use DbContext or something else? Is there some kind of way to start code first but at the same time maintain an edmx model, or those are mutually exclusive?
thanks!
The great thing about EF Code First is that you don't need any scaffolding. You don't need an EDMX model, you don't even need to specify the exact nature of relationships, it's all based on conventions. For example your classes must have a property called Id, which will be taken to be the Primary Key of the table. All string based fields are generated as nvarchar(MAX). Of course some conventions might not be what you want and Code First supports this through pluggable conventions (you can remove most conventions and create your own)
You should do some of the basic tutorials to get an idea of how the Code First flow works as it's an entirely different proposition to the Db First approach.

In C# is it possible to create a model of entities regardless of the database initially?

In C# is it possible to create a model of entities regardless of the database initially. Are there tools to create graphic entities in this way? What do you recommend?
Well, it's in your question as a tag - entity framework is a good tool for this. You can create classes, that you push later to the database.
Search for "code-first". Google will provide plenty of results I'm sure.
Edit based on the comment:
Well, in that case, try this: when you're creating edmx file, pick the second option - empty model.
From its description:
Creates an empty model as a starting point for visually designing a
conceptual model from the toolbox. Classes are generated from the
model when the project is compiled. You can specify a database
connection later to map the conceptual model to the storage model.
this may be the thing you want then.

Generating db schema from c# class

Is there any other method than nHibernate by wich we can generate db schema from class definition? My classes arn't that complex etc (few one-to-many relations). However I would like to just be able to save my objects in db and recreate schema if needed.
I am stuck with .NET 2.0. I am not that particular about performance for this project, I am just lazy to create tables and write save/load code and deel with nHibernate xml.
Thanks
.NET classes are not compatible with a relational data model. That's why ORMs such as NHibernate exist. Without a mapping which describes the conversion of the model to relational tables you cannot create a DB schema. You could take a look at FluentNhibernate automapping feature. If you stick to the conventions it can create the database schema from .NET classes.
NHibernate can do this. The relevant classes -- SchemaExport and SchemaUpdate -- are in the NHibernate.Tool.hbm2ddl namespace. Here's one example.
Yes, ther are other ways. THis is called "programming" - you may have heard of that. Basically, it is possible to write your own database generator. Been there, done that, long before nhibernate had that functionality (or, actually, even existed).
Sit down, write your own database generator.

Categories

Resources