Is it possible to use the Entity framework from a class library?

Asked

Viewed 118 times

1

Normally I use the Entity framework in an MVC project but I would like to know if I can use it from a class library project

  • Explain better what you are trying to do. If I understand correctly, there are no restrictions, if that is your goal.

  • Imagine that I have my model classes in the class library, and then I use EF to create the database I can do this or I need to create the classes within a MVC project

  • 1

    I cannot tell you with full certainty, because both the EF and the MVC are kind of "mafic". But it is possible that it has some problem in the MVC, although it should not, I can not say for unaware of the internal architecture. I guess it depends on how you do it. When you create a project in Visual Studio of a type has nothing very special, it creates a lot of things that you will need. But it is even possible to create a void, which only has the configuration ready to compile properly to operate with a web server.

  • 2

    It can. Nothing prevents.

1 answer

1


Yes, it is totally possible to separate the project into class libraries

even has a question here at SO-PT on performance

Example of how to separate in projects:

In a blank solution create 3 projects, they are

i) Project name.Classes (here will be the project classes);

ii) Project name.Entity (here will be the settings of Entity)

iii) Nomeprojeto.MVC (here will be the web layer)

In the layer of classes, create a class, example:

public class Pessoa
{
    public int PessoaId { get; set; }
    public string Nome { get; set; }
    public string Sexo { get; set; }
}

In the layer Entity install the Entity Install-Package EntityFramework for package manager console or by nuget. Create a context class, example:

public class EntitySeparadoDbContext : DbContext
{
    public EntitySeparadoDbContext()
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //No exemplo está sendo usuário o EntityTypeCOnfiguration para configurar como ficará a classe no banco
        modelBuilder.Configurations.Add(new PessoaConfiguration());
    }

    public DbSet<Pessoa> Pessoas { get; set; }
}

After that you can enable the Migrations (enable migrations), add them (add-migration <nome>) and execute them (update-database)

OBS: It is necessary in the package manager console define the project Entity by default, example: inserir a descrição da imagem aqui

After that, in the layer MVC just instantiate the context and use it normally, example:

private EntitySeparadoDbContext db = new EntitySeparadoDbContext();

public ActionResult Details(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Pessoa pessoa = db.Pessoas.Find(id);
    if (pessoa == null)
    {
        return HttpNotFound();
    }
    return View(pessoa);
}

I did an example project and climbed it in the Github

Maybe you also want to see about:

How to implement the Repository Standard in C# with EF?

What is addiction injection?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.