views:

56

answers:

3

Hi fnds,

There are lof of difference between Linq to Sql and Entity framework like Linq to SQL is one to one mapping and EF is many to many mapping and many other which can be found at http://stackoverflow.com/questions/8676/entity-framework-vs-linq-to-sql

But here I am asking with EF and Linq to Sql, do we create Entities first and then generate tables or the other way and what are positives and negatives of both the approaches. In EF could there be any business entities which are inherited form multiple tables and does not represent any thing in the DB. Is Linq to Entities different from EF?

Please let me know if question is not clear or wrongly asked. Thanks in advance guys.

+2  A: 

For "production" releases, both EF & L2S require that a data model (read: Database schema) be in place before any classes can be generated. The ability to create your classes first and then generate the DB from that is available in the form of the EF4 CTP that ScottGu blogged about recently.

As far as a single Entity reflecting multiple tables: I believe that is currently a one-to-one relationship: 1 table = 1 Entity.

AllenG
Thanks Allen, Yes EF4 can do it both ways. When starting a new project which one should be preferred? and why.
Praneeth
According to Microsoft, when doing .NET 4.0, EF4 is the preferred solution for doing O/RM. Many new technologies will be placed on top of EF (WCF Data Services for instance) and while Microsoft still fixes bugs on L2S, it is not being developed anymore. Or as they call it; it is done ;-)
Steven
A: 

in linq to SQL you first make a database, then import it, build the app and your objects will be made.

i started to fool around with EF at first learning MVC, then at the point of starting to work with the code i used linq to SQL, and, well, it's both linq, there is no real difference in the linq queries that i can tell.

Stefanvds
There certainly are differences. Especially between L2S and EF3.5. EF3.5 for instance does not support `Single` and `SingleOrDefault`. It does not know how to translate queries that have a `Contain` method (for instance: `from c in db.Customers where ids.Contains(c.Id) select c`). Besides this, the performance and readability of the L2S is much, much better. All of this was fixed BTW with EF4.
Steven
+1  A: 

I believe with either EF 4, or an add-on, you can create your model first and have it generate your DB schema. Prior to EF 4, and with Linq-to-SQL you need to start with the DB and map the models.

taylonr
Thanks Taylor, What is preferred approach here?
Praneeth
Not sure there is a "preferred approach." If you already have a DB, I'd use the Linq tools to model it in code. If you're starting a completely new project and don't have the DB, then perhaps the code first approach would be better
taylonr
Indeed: The LINQ to SQL `DataContext` class contains a `CreateDatabase` method that allows you to build a database from a model. L2S does not contain a feature to generate the SQL statements for this, as the EF4 designer has. However, when you use the `Log` property, you'll probably get pretty close.
Steven