user3413723
user3413723

Reputation: 12243

The entity type 'List<PersonAddress>' was not found. Ensure that the entity type has been added to the model

I'm building an application with .net core 2.0. I have a Person object with a list of addresses. Addresses are stored in the Addresses table. I then have a joining table that relates Person to Addresses.

class Person {
    public List<PersonAddress> Addresses { get; set; } 
}

class Address {
    ...address properties...
}

class PersonAddress{
    public int AddressId { get; set; }
    public Address Address { get; set; }

    public int PersonId { get; set; }
    public Person Person { get; set; }
}

When I save a Person object with a PersonAddress that contains an address, it works great. It inserts a new person, a new address, and a new record in the joining table. (I have configured relationships in the fluent api).

When I try to update, however, I have a problem. After loading a person object with a list of their addresses, I can add, remove, or change the addresses, and the changes are not tracked.

I tried this in my controller to get it to process changes:

dbContext.Entry(person.Addresses).State = EntityState.Modified;

And I get this error:

The entity type 'List<PersonAddress>' was not found. Ensure that the entity type has been added to the model.

It is unquestionably on the model, so I don't know why this isn't working. I trid this:

person.Addresses.ForEach(item => _context.Entry(item).State = EntityState.Modified);

And it was ok with it, but this doesn't track adding or removing, so it's not sufficient.

Upvotes: 17

Views: 14337

Answers (3)

user3413723
user3413723

Reputation: 12243

Here is some code to do a diff of existing addresses and the ones the user is submitting.

// first, get a list of the items currently in the database
// make sure to get asNoTracking so we don't run into any weird issues
var currentList = db.Addresses.Where(item => item.PersonId == person.Id)
    .AsNoTracking().Select(item => item.AddressId);
// get a list of the ids in the newly submitted list
var newList = person.Addresses.Select(item => item.AddressId);

var newItemIds = newList.Except(currentList);
var deletedItemIds = currentList.Except(newList);
var updatedItemIds = newList.Intersect(currentList);

// now you have Ids of all that have been added, removed and updated

Upvotes: 0

Andrei Ivanov
Andrei Ivanov

Reputation: 151

My problem was that I had a

var listOfItems = await DbContext.Items.ToListAsync();
DbContext.Remove(listOfItems);

After looking a second (10th) time over it, I realised I needed to change it to:

var listOfItems = await DbContext.Items.ToListAsync();
DbContext.RemoveRange(listOfItems);

So just needed to change from Remove to RemoveRange .

Upvotes: 14

Jay
Jay

Reputation: 327

In my case, the method is asynchronous and I was getting the error because I omitted the "await" keyword. see attached images, might be helpful.

Added the await keyword

Entity type not found error

Upvotes: 0

Related Questions