Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Effort with EF6 in a DB First approach

I'm using the Model First approach with EF6 and I'm trying to use Entity Framework Effort to develop in-memory tests.

Here is what I do in my test:

var inMemoryConnection = Effort.DbConnectionFactory.CreateTransient("name=MyEntities");
var inMemoryContext = new MyEntities(inMemoryConnection);

MyEntities:

public partial class MyEntities: DbContext
{
    public MyEntities(DbConnection dbConnection)
        : base(dbConnection, contextOwnsConnection: true)
    {
    }

When I run the tests, I get an error saying I didn't specify any [key] attributes which is normal since I am not using a Code First approach. Therefor, the OnModelCreating method is called and shouldn't have to.

Is there a way to use Effort in a Model First design without having to add these attributes?

Thanks !

like image 396
Boubou Avatar asked Jan 02 '18 17:01

Boubou


People also ask

What are the three approaches in Entity Framework?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First. This article discusses all these three approaches and their pros and cons.

Which approach is best in Entity Framework?

As in this diagram, if we already have domain classes, the Code First approach is best suited for our application. The same as if we have a database, Database First is a good option. If we don't have model classes and a database and require a visual entity designer tool then Model First is best suited.

Which is the type used in Entity Framework to load data?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.


1 Answers

I found my mistake.

Turns out Effort.DbConnectionFactory.CreateTransient is used for Code-First.

Instead if you're working with a .edmx, Model-First, it is Effort.EntityConnectionFactory.CreateTransient("name=MyEntities") you have to use.

like image 77
Boubou Avatar answered Dec 10 '22 06:12

Boubou