0

i am working on a big Project, where an IIS-Application, a WCF-Service and a normal application have access to the same database with the same model-classes. Now I`d like to ask if and how it is possible to put the DBContext in a class-library so that every other Project can use that context.

Here is a small part of my DB_Entities in the class library:

using ManagementLibrary.Model;
using System.Data.Entity;

namespace ManagementLibrary.EntityFramework
{
    public class DB_Entities : DbContext
    {
        public DB_Entities() : base(nameOrConnectionString: "DatabaseConnection") { }

        public DbSet<User> Users{ get; set; }
        // more sets here
    }
}

I tried to insert the Connection-string and configuration in the App.config of my Client-Application:

Registering the Provider:

<system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
</system.data>

Adding the Connection String:

<connectionStrings>
    <add name="DatabaseConnection" connectionString="server=192.168.0.2;Port=5432;user id=postgres;password=postgres;database=ManagementDatabase" providerName="Npgsql" />
</connectionStrings>

The rest of the EF-Config:

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
</entityFramework>

Now, when I want to add the migrations, it does not work.

PM> Enable-Migrations
No context type was found in the assembly 'ConsoleTestClient'.

There is even something more complicadet: In my Class Library there is a controller-class which has the DB_Entities as an Attribute. I want to use that controller-class in each Project, so that i have to write the rules for the database-operations just once. The controller-class is very big and contains many methods, here is just a very small part so that you can see what i want to do:

public class UserController: Controller
{
    public User insertUser(User newUser)
    {
        entities.Users.Add(newUser);
    }

    //a lot of more methods here
}

The Controller is an Abstract class which contains a private attribute entities (Type: DB_Entities).

So, a lot of text for this small question: How do I have to configure my Projects to be able to use all my Controllers and DB_Entities that are in a class library?

8
  • Can you put entity code behind rest api call Commented Aug 7, 2017 at 8:19
  • what is a rest api call in that case? Commented Aug 7, 2017 at 8:21
  • You can use wcf service with webhttp binding endpoint also. Commented Aug 7, 2017 at 8:23
  • so you mean making one WCF-Service where the IIS-Site and the management-application have access? Commented Aug 7, 2017 at 8:35
  • Yes. Something like that will straight work in your case otherwise, you will need to create a shared dll containing everything and copying and referencing it in every project. Commented Aug 7, 2017 at 8:37

3 Answers 3

1

You can put entity code behind rest api call. You can use wcf service with webhttp binding endpoint also.Yes. Something like that will straight work in your case otherwise, you will need to create a shared dll containing everything and copying and referencing it in every project. For shared dll,you will need to create separate c# project for dll. In that solution, add a console app to test your calls. When you build your project, dll will get generated in debug or release folder. When putting EF behind WCF call, do set lazy loading and proxy creation to false.

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

Comments

0

I'm not sure if you can create shared class library with EF and use the add-migration function against it since DLL is already compiled and it could not be edited as far as I know, every time you add-migration it creates a new class in the Migration folder.
But you could try a few scenarios here.

  1. As Amit wrote, you could try to build another solution with API. Then you could call that API from all projects: WEB, WCF or Mobile.

or

  1. You could have one project for you DB which will have public repositories which would expose their interfaces. Then you need to build it and reference the DLL in all your projects where you need a connection to DB.

Or if you need access to entities then you could create an interface for DBContext and put all of it's DbSets in it.

Comments

-1

on this case is beter try Microservice architecture because in big project for project development is need to break to mini project and other project by easily can access to main project(Context Project). I suggest you see the flowing links.

Microservice in microSoft

Microservices Architecture Udemy course

1 Comment

Microservices have nothing at all to do with ORMs and Unit-of-Work. Using multiple smaller services increases the temptation to share classes and models. Besides, the share schema and contract, not class guidance came from SOA, not microservices. Almost everything you've heard about microservices is actually repackaged SOA guidance (except containers)

Your Answer

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