0

I've got an ASP.NET MVC 4 site with MSSQL database generated by code first approach. I want to use this database in another project. This project should crawl several tables from db, update them and send notification to users.

What is the best way to add existing database from the first project to the second project? I'm thinking about generating edmx by database, but this approach doesn't seem good enough.

5
  • generating edmx by database means database first... It's perfectly valid, what's your problem ? Commented Oct 24, 2012 at 18:13
  • Is your other application a web application as well? Commented Oct 24, 2012 at 18:17
  • 1
    Can't you just move your DbContext to a library and use it in both projects? Commented Oct 24, 2012 at 18:42
  • @Pluc I think it will be a windows service project, because it should look at db each 30 minutes and if something was changed then it would send notification to users. Commented Oct 24, 2012 at 18:43
  • @RaphaëlAlthaus I want to use DbContext. I think it's more comfortable. but DB first approach should use ..Entries. Or I'm wrong? Commented Oct 24, 2012 at 18:45

3 Answers 3

2

You would be best moving the EF code and Entities into their own library, and then having your two different projects (Web and Windows Service) have a reference to that library.

Then, if and when your database structure or entities change, you only need to do this in one place.

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

1 Comment

This is a good solution, but I want to use DbContext instead of ..Entries. Can I make a separate DbContext library and then use it in ASP.NET MVC 4? I'm afraid of some uncommon problem. But it should be a good solution from the first look.
1

If the second project is not going to modify the structure of the database, then I would use a data-first approach in your second project.

5 Comments

I think in this direction too. It seems natural to use in one project code first and in another data first, because we have prepared data in another project. But I've got an issue with DbContext & ..Entries (see my comment to RaphaëlAlthaus below the question).
Db First can use dbContext, you just need to use the code generator for it on your edmx model :)
It's a good news! Could you explain me how to do it? If I generate edmx by database then it makes me ..Entities class. How can I get an DbContext like in me first project?
Open your model then right click on it, you will see a menu item that says "Add Code Generation Item..." click on that and it will bring up the code generation item display, pick EF5.xDbContextGenerator and then save your model.
thx! It works fine. A good article for "next generations": msdn.microsoft.com/en-us/data/jj206878
0

You can use codefirst mapping without problem on existing database. So simple reference assembly with datacontext in other project and it will work. (Don't forget set connection string in config file of other project)

2 Comments

Could I have a reference to my entire MVC 4 project or should I make a third project and place a db-code in it?
You should move your dbcontext and all mapping to different project (as is) and reference it from both project mvc and new project.

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.