4

If I have this project structure

  • Foo.Data
    • reference EntityFramework
  • Foo.Business
    • reference Foo.Data
  • Foo.Web
    • reference Foo.Business

Isn't that supposed to allow me to prevent adding a reference to EntityFramework from Foo.Web?

How can I call System.Data.Entity.Database.SetInitializer() from my global.asax.cs without adding the EntityFramework reference?

3 Answers 3

3

Why do you want to?

The reason you're doing this uncoupling is (I assume) to enable you to switch out the data tier at a later point, without having to modify anything in the Web project, and as little as possible in the Business project. To accomplish this, you should make sure that all your classes work against interfaces, rather than against concrete implementations.

In your example, you should probably define a Repository interface of some sort, which includes an Initialize() method. You then create a class (perhaps your specialized DbContext) implement the interface, and you work against that. In the Initialize() method on your repository, you call Database.SetInitializer() and thus you never have to reference System.Data.Entity in either the web or business projects.

Sign up to request clarification or add additional context in comments.

Comments

2

What you can do is create a InitializeDatabase() function in your Foo.Business project which in-turn calls System.Data.Entity.Database.SetInitializer(). You can then call InitializeDatabase() from your Foo.Web project which already has a reference to Foo.Business

2 Comments

Can you elaborate on why/how it didn't work? What error did you get?
I figured it out. I was injecting it incorrectly. This approach worked.
2

Nope. If Foo.Web needs classes in EntityFramework, it will have to reference it. References don't cascade between projects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.