I'm doing a project with ASP.net core. I found that most of developers use Entity Framework for dealing with the database. I have worked with raw SQL queries with ASP.NET. I'm using SQL SERVER 2014 and have to develop a RESTfull API to deal with my database. My database consists of about 50 tables.
So my question is, what method is better for my task, using Entity Framework or using raw SQL queries?
From my research I have found that Entity Framework has basically two methods, Code first and Database first. I have looked into both methods. I need to know pros and cons of using entity framework compared to normal SQL queries.
And finally is there a effect on .net core with entity framework?
It seems that you have a large application, my recommendation is that you should use Entity Framework because you have LINQ. (Examples here: http://www.entityframeworktutorial.net/Querying-with-EDM.aspx).
With LINQ you can build your queries using methods instead of strings.
With LINQ:
var L2EQuery = context.Students.where(s => s.StudentName == "Bill");
With Native SQL:
var studentName = ctx.Students
.SqlQuery("Select studentid, studentname, standardId
from Student
where studentname='Bill'")
.FirstOrDefault<Student>();
Using entity framework with code first migrations and a decoupled from ef repository pattern architecture does not restrict you from using sql queries as well sometimes when you need to.
Your question will be closed because it is too opinion oriented.
But yes, personally, I greatly encourage the use of Entity Framework.
The thing is, if you have no experience at all in EF, then you will be subject to a learning curve. Which means it will still be easier for you to simply use SQL query in your code. That said, if you take your time and learn correctly how to use EF it will probably be of good use later.
I've experienced both Entity Framework and raw SQL queries, and now that i'm used to EF, I find it much better.
Related
I want to create a web application, all actions (Create, Update, Delete) should be done using SQL stored procedure. Every action in the web just calls a stored procedure and receives json data to render the view.
So what is the best framework that I can use? Please help
So what is the best .Net framework that I can use?
Every .Net Framework has ways to retrieve data from database. So, it doesn't really matter what version of .NET Framework you use.
I believe you would like to know what kind of library you should use for Store Procedures. If so, you might want to look at Dapper ORM.
Dapper ORM is created by Stack Exchange, and used in a lot of sites including Stack Overflow.
It basically is a wrapper around ADO.NET to map SQL result to strongly type object. If you have to do the mapping manually, it is very tedious and error prone process. So, I highly recommend using Dapper ORM if you have to call Store Procedures.
Just like to ask, I want to create a blog with ASP.NET as a practice to get better. I came from PHP btw.
Anyway I'm stuck because I don't know what's the way most .NET programmers use to connect to the database. I'm planning on using LINQ.
Any tutorial/books and also websites that would point me to the right dorection would really help. Thanks a lot!
The way most .NET programmers use to connect to databases is ADO.NET, included with the Microsoft .NET Framework. Here some useful code examples.
LINQ ist not designed as a replacement for ADO.NET, LINQ provides a uniform programming model for any kind of data. With it, you can query and manipulate data by using a model that is independent of data sources.
var query =
from c in Customers
where c.Country == "Usa"
select c.CompanyName;
foreach (string name in query)
Console.WriteLine(name);
.NET developers use ADO.NET to connect to relational databases. This is the low level API. Entity Framework provides an ORM on the top of ADO.NET that could map relational tables to objects.
If you are new i will advise you to start with linq, but before that you should learn a little bit about ADO.Net, and whats better then msdn tutorials:
ADO.Net:
http://msdn.microsoft.com/en-us/library/h43ks021%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/cc161165.aspx
Linq:
http://msdn.microsoft.com/en-us/library/bb386964.aspx
http://www.codeproject.com/Articles/246861/LINQ-to-Entities-Basic-Concepts-and-Features
I read that in some books they use linq to sql and entity framework..
Whats so good about them?!!?
They are slow.. especially linq to sql. Does entity framework have any benefit that make it worth learning?!?
I strive to make the site as fast as I can, and I dont use disconnected data access much
consider that my goal is to make the site run as fast as it can
ORM frameworks like Linq-to-Sql and Entity Framework will almost always implement an abstraction layer between the program code and database SQL statement. This layer's job is to parse the code's syntax into a SQL statement.
So, if your ultimate goal is increased speed, it would be smart to avoid that layer.
I have been asking my self this question from the time i started workin in the software development field,
What is the best way to access data?
Which gives the best performance?
Which is the best for maintainability?
There are lots of solutions to deal with database in the Asp.Net web app,
Entity framework 4.0
Classes generator using ADO.Net such as Code Author ( i liked the way it works and the way it accesses the database using the data access block in Microsoft Enterprise Library).
i will start a new project tomorrow,and i don't know which approach is better??Any idea?
I'm using Castle Active Record for data access in our new ASP.NET project. It's a great and easy tool that is built upon NHibernate. NHibernate itself is a great ORM for .Net.
There is no "best". There is only preference. Some like MVC, some like WebForms, some like Dynamic Data. Entity Framework works nicely, as does LINQ to SQL(although I hear that one is being deprecated, but I'm fuzzy on that.) All work well.
Personally, I like WebForms, but if I want a quick and dirty CRUD app, I always opt for Dynamic Data, and if I need additional functionality mixed in, I can throw in some standard WebForm aspx pages.
Performance wise, I don't know that there's inherently a big difference. All use the same code under the hood. All are based on ADO.NET. Entity Framework and LINQ to SQL seem to have additional overhead compared to the old-fashioned WebForms, but proper DB design and planning is probably of greater importance.
I'm building a new web application project and am confused by the numerous methods of performing data access. I'm backending on SQL and a bit confused whether to use LINQ to SQL or trtaditional ADO.net ?
what are the advantages and disadvantages of using LINQ/SQL over ADO.net?
If it is ADO.net,then what is the best way to retrieve data means either calling the stored procedures or directly calling the t-sql code?
My question is what is cleanes and most effiecient and professional way of creating DAL for webapplication in asp.net?
Thanks
What are the advantages and
disadvantages of using LINQ/SQL over
ADO.net?
Linq2sql generates a series of
classes that are 1-to-1 mappings of
your (selected) database tables - this means you don't have to write tedious and error prone data access code
using ado.net yourself.
Linq2sql may not provide enough value for you if you intend on using a custom object-to-relational mapping (non 1-to-1) - of course you could still use linq2sql, but it would mean having an extra layer in between.
Linq2sql allows you to easily query the database using powerful linq expressions. Writing linq queries provides you with intellisense that you wouldn't get if you embedded your queries as strings inside ado.net commands, or wrote stored procs in management studio.
Using linq, you don't need to know t-sql while you will if you use ado.net (although it can definitely an advantage if your linq queries start doing strange things!). An example of this the complexity of writing t-sql queries that provide paging resultsets simply becomes .Skip(page * size).Take(size).
Linq2sql automatically creates t-sql that uses parameterised queries which is much more secure against sql injection attacks than handwritten ado.net code which builds up a query using a string.
Linq2sql doesn't work very well with stored procedures - you are probably better off not bothering with linq2sql if using sprocs.
Linq2sql could require your database tables to be less-tightly locked down than would be possible writing ado.net code using stored procedures.
If it is ADO.net,then what is the best
way to retrieve data means either
calling the stored procedures or
directly calling the t-sql code?
If you'd ruled out linq2sql, and ado.net happened to the better choice for data retrieval, I would be surprised if you were directly calling t-sql code very often or even at all. I would almost certainly expect you to be using stored procedures for reasons that you have queries that are too complex using linq, and/or security requirements.
My question is what is cleanest and most effiecient and professional way of creating DAL for webapplication in asp.net?
In my opinion, the cleanest DAL would probably use linq2sql as it is the lightest and most targeted ORM for SQL Server (assuming your still interested in SQL Server for this specific question of course).
The most efficient could be the handwritten one using ado.net, but this is probably a waste of time as more often than not, you will find a tool such as linq2sql writing better queries than 90% of developers.
In my opinion, the most professional DAL could be linq2sql, but it is more likely to be the Entity Framework of NHibernate (as other answers have suggested) due to more flexibility.
My last choice DAL in terms of cleanliness and professionalism would definitely be a handwritten ado.net one.
The best way to go is O/RM. Small apps Linq2Sql, larger apps Entity Framework 4 or NHibernate (Fluent NHibernate).
Calling SPs from your code means that your app logic is placed somewhere else than in the app code. It's a way to go but at present less and less popular because of TDD.
The best way is to create DAL into a separated logic layer, own assembly.
I would without doubt go for Linq2Sql.
Download Linqpad and play around with the included samples to get started.
You should check out some ORM frameworks, like NHibernate: http://nhibernate.info
If you want efficient data access in terms of performance than there is nothing faster than pure ADO.NET. You chan check it out here: http://ormbattle.net/.