I have a WCF application in C# .NET 4.0. I made all my entity classes and can query a sample from the WCF. The sample is just hard coded values.
Now I am ready to persist these in the database. I am lost on how to approach this though. I plan to create tables for each entity class I created, but what is the best way to add the persistent data layer to my existing WCF application. Is Entity Framework a good choice for this? Thanks for any help or suggestions.
What I suggest is to use entity framework code first. It prevents you from re-creating the model/database by hand. Just set it up so that your current entity classes are mapped to the entity framework and it will automatically create the database for you.
If you google entity framework code first, i'm sure u cant miss it.
Related
Newb here sorry. I'm reading up Entity Framework Core, npgsql and PostgreSQL (new to all of these things!!) Can someone please very kindly explain or point me where to read: I am trying to create a brand new database using code.
From the things I have read, I am creating the DB using pgadmin or script directly. What I want to do is create a database all via code (so the user can install Postgresql and my website but not have to worry about any direct SQL commands with PostgreSQL).
Thank you!
There are two ways to have EF Core create the database:
Use the lightweight EnsureCreated() method, which simply creates your database base on the model. If you're just starting out, it's probably best to start here, see the docs.
Use migrations, which also take care of updating your database schema as your code model changes. For example, if you add a property to one of your C# classes, EF Core can automatically generate a migration that will add the corresponding column to the table. Here are the docs for that.
The general docs for this are here. As a general rule, read and understand the EF Core docs first, then you can look at database- and provider-specific details (e.g. PostgreSQL/Npgsql).
I have used code first method before, but somehow I could not think of a way in making my Code First works. I have developed the POCO and DbContext. But now I am stuck on how to get the database created and subsequently used in my application. Do I need to add something in the project?
Currently default provider System.Data.SQLite doesn't support database creation. To have ability to create database programatically from your POCO model objects you need to consider to use for example Devart's SQLite provide. But this is paid solution.
For more information see answer for the following stack overflow question.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
EF 4.1 Code-first vs Model/Database-first
i made some research, but answers did not satisfy me.
I started my own asp.net mvc application with entity framework, to work with database. I started by making a db, than i create model from that database. Well i know thats called "model-first". I know the second way, of doing it from school (code first). My lecturer told me today, that model-first method is rly outdated. Well is that true? Or maybe: does code-first have any advantage? Whats the real difference, which one is better, in your opinion?
Specifically with EF code first refers to the code and model first refers to the database.
which is better really depends on the project. If the db is treated as the core of the application, or you are using db specific features, than a model first approach makes sense.
If the domain model is treated as the core of the application, leaving the db to be just a form of persistent storage, than a code first approach is practical.
as a side note: this concept is usually referred to as model first (the domain/code) and db first (the db schema). with EF this is somewhat confusing as model refers to the db schema, not the domain model and code refers to the code.
DB first is not outdated at all. In fact, for many corporate projects that's the way to go.
If you are building something for yourself from scratch, code first will probably be a better approach.
Entity Framework provides: 1)Database first, 2)Model first and 3) Code first.
1 & 2 Creates the edmx file
Database first: Entity Data Model(edmx:StorageModels) is created from an existing database.
Model first: Conceptual Data Model(edmx:ConceptualModels) is created with designer and mappings specified. Database will be generated from this.
Code first: Create your .NET classes(no .edmx file). Database will be generated from this.
'Old school' would be using ADO.NET DataSet/DataTable.
Fastest data access is using DataReader and writing your own data access.
No framework will be faster than using a DataReader to populate your own POCOs.
Background:
I started to create logical database model for ASP.NET MVC web site. I used visual designer for Entity framework that ships with VS because I have used it before.
But now I already have 33 classes and I'm not finished (including quite some inheritance and a lot of associations). I'm afraid that it would be too complicated and time consuming for me to manually set all the table mappings and than generate database tables. I've no experience with it - I've done it the other way: classes from database tables and it took me a lot of time to get it work in a smaller project.
Question:
How can I easily and quickly create database tables for logical model (class diagram) in .NET / VS ? It would be great if it was possible automatically. I have never worked with LinqToSQL visual designer and it seems to be no reference on the web on how to create database tables from LinqToSQL classes. Is it possible at all ? If not is there any way to create database tables with Entity framework automatically - without having to specify table mappings ?
And one side question: if I used LinqToSQL classes are that going to commit changes to database every time I change properties ? Or is some caching taking place there ?
Entity framework has a concept called "Model First", which generates the database model from you model, hence the name.
You can read about that here: http://msdn.microsoft.com/en-us/data/ff830362
However, my personal favourite when it comes to Object Relational Mappers is NHibernate with the addition Fluent NHibernate. They have a concept where you work with your domain model rather than you data model and you use conventions to control your mappings. It's pretty neat. You can get started with some pretty good examples by looking at this code here: https://github.com/sharparchitecture/Northwind/tree/master/app
Linq2Sql is too limited for the case you are talking about. And it has no capability to generate data models from code. In fact, Linq2Sql works the other way around - it generates a set of classes from your data model, much like Entity Framework also can do.
Neither Linq 2 SQL or Entity Framework commit anything until you explicitly choose to do so. They both have a notion of a object context which keeps track of all changes made. When you call "Save", they transform those changes into SQL which is then executed in the database.
Like MikeEast, I've had a very good experience with Fluent NHibernate.
On my project, I use the Automapping feature, which allows me to change my data model almost at will, and the database schema automagically gets updated.
No SQL, no worrying about foreign keys, etc, etc, etc - I love it!
Fluent NHibernate Automapping
Finally I have sticked with Entity framework - tables generating is really plainless once I learnt how to deal with database connections...
i want to relate each Field of an Entity Class to the corresponding datatable Field.
im working on c# currently
Any Suggestions?
The Entity Framework does exactly that, and very well. It also does a whole lot more such as let you write strong typed LINQ queries directly from your application code.
your other good options are NHibernate or Linq to SQL
this class of frameworks is generally called Object Relational Mapper (ORM)
I would say Entity Framework offers the most conveniences for beginners such as visually configuring your data model. it can infer your object model from your database or it can create a database for you from your object model.
The new Entity Framework Code First approach is incredibly simple and powerful in my opinion
Link
It is what entity framework is doing, isn't it?
Microsoft's ADO.NET team just published an Entity Framework Beginners Guide at their team blog:
Link