Linq-to-SQL and Entity Framework in the same project?

domain-driven-design entity-framework entity-framework-6 linq-to-sql service-layer


Right now, I'm attempting to use the DDD methodology to establish separation of concerns in a web application.

My UI, Service Layer, and Repository Layer must be loosely coupled if I'm to perform unit testing.

Entity Framework will let me separate my domain contexts and entities, therefore I've made the decision to utilize it for the repository layer.

The web app currently makes use of Linq-to-SQL.

Are there any significant disadvantages or warning signs I should be aware of before integrating EF and L2S into the same web app? Or even before employing this strategy in general?

Moving forward, we want to use EF and gradually move the code as we work on it, separating it into the appropriate domain contexts.

2/3/2016 9:39:01 PM

Popular Answer

Both ORMs can be used in the same project without any issues. But there are several things that can make it simpler.

  • Connect using just one string. By default, Linq-to-Sql adds a connection string item with an extremely long name to the application's configuration file, such asLinqToSql.Properties.Settings.MyDataContextConnectionString . I would substitute the EF connection string for this one. You must use the constructor, which accepts a connection string parameter, to generate DataContext instances:

    var connString = ConfigurationManager.ConnectionStrings["MyDbContext"].ConnectionString;
    using (var db = new MyDataContext(connString))

    Of course, wrapping this up in a context factory is simpler.

  • Use distinct namespaces. Linq-to-Sql and EF may (temporarily) utilize the identical class names, particularly in a transition scenario (either generated, or created by yourself if you work code-first). If you can quickly identify the appropriate classes in intellisense, it is really convenient.

  • As an alternative, you might think about keeping the EF and Linq-to-Sql code in different projects.

2/3/2016 10:26:17 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow