A
A
Alexey Pavlov2016-04-16 22:53:32
C++ / C#
Alexey Pavlov, 2016-04-16 22:53:32

Is it possible to use EntityFramework only in Class Library?

I want to hide the work with EntityFramework in a class library so that it is not visible from the main project (encapsulate it). Now, to work with EF, you have to add both EntityFramework and EntityFramework.SqlServer to the references of the main project, as well as specify its settings in App.config.
At the moment, EF6 is interesting, but EF7 is also interesting.
PS It is desirable to make the database data visible in the studio in the WPF project in design mode.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2016-04-17
@byme

You can't, but you must! This is a three-tier architecture.
Those. we have a certain frontend, business logic and a certain repository that works with the database (and not only). I only see its interface and we have no idea what is there: Entity Framework, ADO.Net, or the like.

A
Alexey Pavlov, 2016-04-17
@lexxpavlov

The solution is described in the article . The database settings are stored in the code, nothing needs to be written in the App.config file of the EF settings library.

using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.SqlServer;
using Models;

namespace DatabaseLibrary
{
    public class MyConfiguration : DbConfiguration
    {
        public MyConfiguration()
        {
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
            SetDefaultConnectionFactory(new LocalDbConnectionFactory("v11.0"));
        }
    }

    [DbConfigurationType(typeof(MyConfiguration))]
    public class Database : DbContext
    {
        private const string ConnectionString = @"мой connection string";

        public Database() : base(ConnectionString)
        {            
        }

        // мои DbSet-ы
    }
}

It remains to decide how to use the data in Design mode in a WPF project.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question