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

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

Question

I am currently trying to implement separation of concerns using the DDD approach in a web app.

I want to be able to implement unit testing, so loosely coupling my UI, Service Layer, and Repository Layer are key factors.

I have decided I want to use Entity Framework for the repository layer since it will allow me to split my domain contexts and entities.

We currently use Linq-to-SQL in the web app.

Are there any major drawbacks or red flags that I need to be considering before putting EF and L2S in the same web app? Or before taking this approach in general for that matter?

The intention is to use EF going forward and slowly migrate code as we touch it, splitting things into their correct domain contexts.

1
2
2/3/2016 9:39:01 PM

Popular Answer

You can use both ORMs in one project without any problems. There are a few things that can make it easier though.

  • Use one connection string. By default, Linq-to-Sql creates a connection string entry in the application's config file, which has a very long name, like LinqToSql.Properties.Settings.MyDataContextConnectionString. I would remove this connection string and use the one for EF. You'll have to create DataContext instances by using the constructor that takes a connection string parameter:

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

    Of course it's easier to wrap this into a context factory.

  • Use clear namespaces. Especially in a transition scenario, Linq-to-Sql and EF may (temporarily) use the same class names (either generated, or created by yourself if you work code-first). It is very convenient if you can easily recognize the right classes in intellisense.

  • Alternatively, you may consider keeping the Linq-to-Sql and EF code in separate projects.

3
2/3/2016 10:26:17 PM


Related Questions





Related

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