Once again LINQ to SQL or Entity Framework for new

2019-05-14 08:46发布

问题:

first of all, my background is: pretty large database (~100 tables with ~10-20 columns each) on MS SQL Server and it's always gonna be SQL Server and database first.

i have some experience in WebForms and LINQ to SQL, but i'm sick of constructing forms for editing all that mentioned tables, so mvc 3 razor with it's Scaffolding just hypnotized me, and i'm dreaming of it generates all that forms for me

but it comes out (or, maybe, i don't get something), that scaffolding works only for EF DbContext. for L2S it says 'unsupported DbContext'. i've tried MvcScaffolding with LinqToSqlScaffolding (typed 'Set-DefaultScaffolder Repository LinqToSqlScaffolding.Repository' in PM console), but still not a sing of L2S related mapping.

So the first question is - "Is there a way (a stable way) to get scaffolding for Linq to SQL classes"

now i've had investigated some and found all of that topics about L2S vs EF, saying the same - "L2S came first, so it's more stable, and EF is still young, but it's a long shot, it's flexible, mappable and so on".

the first thing i don't get is how should i map that flexible model for my enterprise database? i've used a designer to generate class for EF DbContext, as i've done before for L2S, and as for L2S it consist of ~42000 lines. now i have to search for some entity properties there and set some attributes for them , like 'Required', 'Display' and 'Range', but designer will overwrite them. i know how to implement partial class or partial method, but not aware of making any additions to properties in external file, for designer couldn't overwrite them. i guess, this should be the second question.

and if i should map my enterprise database to EF by hands, to keep it flexible... o_O that ~42000 lines of auto-generated class scares me to bee-gees! is it really necessary?

回答1:

  1. Entity Framework has advanced quite a bit lately. I'd consider it "stable". The new integration with MVC3 is outstanding.
  2. As for adding custom DataAnnotation attributes for validation, this article describes an approach using EF. (Scroll down to the bottom.) Since you can't modify the generated properties, it uses a custom metadata mapping to apply them.
  3. You really want to let it generate as much as possible for you, to keep from going insane. EF has a design surface very similar to L2S and you can get as simple or as complicated as you want.

Also, MVC3 will generate strongly-typed views for you based on any object model. The primary difference is that, with EF, it will wire up EVERYTHING for you, whereas with custom POCO or L2S you have to do all of the plumbing yourself. (From that standpoint, this works exactly like earlier versions of MVC.)



回答2:

With my limited experience, I feel, calling EF directly from controller is not looking good. I think, database should be accessed through a separate layer. I prefer controller calling a WCF service which can talk to database using ADo.NET/LINQtoSQL/EF/NHibernate,etc.

Suggestions and disagreements are welocme. But please don't downvote - its my thought only. :-)

References:

  1. WCF Service with asp.net mvc application