C# class to Sql table - c#

There are a lot of Sql table -> C# class methodologies, but I'm looking for the reverse.
Hypothetical situation:
I have N classes populated by some web service I consume, manipulate, then preform an action on. Now the boss wants said web service data persisted in a database.
I already have the classes defined, how can I quickly and easily (aka, lazily) generate a sql table off of each class?

Entity Framework and nHibernate both allow you to use them in "code-first" mode, which involves writing classes then generating databases.
There is a walk-through of this on ScottGu's blog here: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

you can use MS Entity Framework Code First approach:
Code First allows you to define your model using C# or VB.Net classes,
optionally additional configuration can be performed using attributes
on your classes and properties or by using a Fluent API. Your model
can be used to generate a database schema or to map to an existing
database.
read more about it here: EF 4.1 Code First Walkthrough

Entity framework 4 has a code first development feature, I haven't used it so can't really recommend it
ScottGu blog

Related

C# interview questions approaches in entity Framework?

In my recent interview, my interviewer we asked about approaches in entity Framework I told them code first and table first. Is there any approaches pending.
We can use three type of entity framework approach as per project requirement.
Database First:
An existing database can be used
Code can be auto-generated.
Extensible using partial classes/ T4 templates
The developer can update the database manually
There is a very good designer, which sync with the underlining database
http://www.entityframeworktutorial.net/database-first-with-entity-framework.aspx
Code First:
There is full control of the model from the Code; no EDMX/designer
No manual intervention to DB is required
The database is used for data only
http://www.entityframeworktutorial.net/code-first/what-is-code-first.aspx
Model First:
Good support with EDMX designer
We can visually create the database model
EF generates the Code and database script
Extensible through partial classes

Can now use BreezeJS without having to use EF?

Previously you had to use Entity Framework as Breeze connected directly to the DbContext and that object did not exist elsewhere.
There is the notion of creating Metadata by hand(ie by T4)
I have access to the SQL server where every Table has its own crud usp (SSMS Tools Pack) the ashx does all the RMI into the DB, generates the json schema etc and the DTO service model. I have looked at WCF service layer (http://davybrion.github.io/Agatha/) but monolith EF seems to be everywhere. I have tried Angular I am quite happy to use ADO or Dapper.NET is there connectivity for BreezeJS is to a high performance back-end (Micro-ORM) or should I use Kendo DataSource (http://docs.telerik.com/kendo-ui/framework/datasource/overview). This is for a Hybrid Mobile App, that need frictionless data. Anyone else found an easy ClientSide/Server Side JSON Data integration system that is not so bloated?
Thanks in advance
Yes, you can use Breeze without EF. This needs to be promoted better.
The Breeze.ContextProvider package does not depend on EF. It has a ContextProvider class that handles turning the JSON from the client into server-side .NET entities. You subclass ContextProvider to implement the part that does the actual saving to the database.
breeze.server.net provides two implementations: Breeze.ContextProvider.EF for Entity Framework, and Breeze.ContextProvider.NH for NHibernate. You can look at these for inspiration about how to build the Dapper implementation.
One of the tricky bits is performing the add and delete operations in the right order. For instance, if I'm adding a Customer and some related Orders, the Customer needs to be added to the DB before the Orders. EF sorts the adds automatically, but NH does not, so we have a SortDependencies() method in NHRelationshipFixer. You may need to do something similar if your micro-ORM does not do it for you.
If you come up with an implementation for a micro-ORM, please consider contributing it to the community.

entity framework clarification about model objects and their methods

I had a conceptual question about EF.
I am pretty new to the idea of an ORM in general, so I wanted to get some clarification on somethings:
I have an existing database, and I want to convert the data I am pulling from that data into objects that my application can interact with as objects, rather than just data.
An ORM will accomplish that, correct?
In EF can I create methods specific to my objects? For instance, can I make it so my application can call something like employee.ViewDetails() Where employee is an object?
If so, is there a simple tutorial you could recommend?
Is EF portable between applications? Meaning, is it easy to build an EF structure and then port it to multiple applications?
I can just do that by referencing it from different solutions, no?
Thanks for all the help
Before Answering your Question let me give you short brief about Entity Framework
Using the Entity Framework to write data-oriented applications provides the following benefits:
Reduced development time: the framework provides the core data access capabilities so developers can concentrate on application logic.
Developers can work in terms of a more application-centric object model, including types with inheritance, complex members, and relationships. In .NET Framework 4, the Entity Framework also supports Persistence Ignorance through Plain Old CLR Objects (POCO) entities.
Applications are freed from hard-coded dependencies on a particular data engine or storage schema by supporting a conceptual model that is independent of the physical/storage model.
Mappings between the object model and the storage-specific schema can change without changing the application code.
Language-Integrated Query support (called LINQ to Entities) provides IntelliSense and compile-time syntax validation for writing queries against a conceptual model.
Going Back to your first Question
Yes
Entity framework is useful in three scenarios.
1- First, if you already have existing database or you want to design your database ahead of other parts of the application. (Which is your current case)
2- Second, you want to focus on your domain classes and then create the database from your domain classes.
3- Third, you want to design your database schema on the visual designer and then create the database and classes.
2) in EF can I create methods specific to my objects? For instance, can I make it so my application can call something like employee.ViewDetails() where an employee is an object? If so, is there a simple tutorial you could recommend?
Yes Sure Take a look on this:
- https://msdn.microsoft.com/en-us/library/dd456847.aspx
- http://www.asp.net/mvc/overview/older-versions-1/models-data/creating-model-classes-with-the-entity-framework-cs
3) Is EF portable between applications? Meaning, is it easy to build an EF structure and then port it to multiple applications? I can just do that by referencing it from different solutions?
you might need to Implementing the Repository Patterns
Have a look at this Amazing tutorial
http://blog.gauffin.org/2013/01/repository-pattern-done-right/
http://rycole.com/2014/01/25/entity-framework-repository-pattern.html
Hope this helps!
Wish you the best :)
Blubberbo,
There are various ways to work with EF, they are Code First, Model First and Database first.
More information can be found in the below SO post.
Code-first vs Model/Database-first
1) You can use LINQ to SQL or LINQ Objects in context with an ORM like EF to interact with Objects.
2) If you would like methods specific to specific types, then you might want to take a look at writing extension methods for specific types.
More information here
https://msdn.microsoft.com/en-us/library/bb311042.aspx
3) To make it portable, you might want to build abstractions around it, like for example you might want to write repository classes to separate the DB layer that uses EF and the layer that calls it(a.k.a the repository layer).
More info can be found here
http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
Hope that helps,
Praveen Raju

Entity Framework destriong my classes

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.

Manual object mapping in ado.net entity

I writing data access layer using ado.net entity. But I want do it by manual (not generate code by tool).
I need help how to do it, how many step and example.
You may want to have a look at Entity Framework Code First. See EF 4.1 Code First Walkthrough

Categories

Resources