0

I have a strange problem with EntityFraemwork when I want to update entity. I have User entity and Searchfilter entity, they have one-to-many relationship

public class User : IdentityUser
{
   public virtual List<SearchFilter> SearchFilters { get; set; }
}

public class SearchFilter : BaseEntity, IAggregateRoot
{
   public virtual User User { get; set; }
}

1 - I create user 2 - I run method that creates and adds user to searchfilter

SearchFilter searchFilter = SearchFilterRepository.AddAsync(new SearchFilter
{
     SearchInDescription = false,
     SearchInTitle = false
 }).Result;
  

I used Result without async that had a test. I thought the problem was in async operations in EF. Then:

 searchFilter.User = user;
 SearchFilterRepository.Update(searchFilter);

Update:

public void Update(T entity)
{
    _dbMainContext.Entry(entity).State = EntityState.Modified;
    _dbMainContext.SaveChanges();
}
 

And I have an error: The "PK_AspNetUsers" PRIMARY KEY constraint was violated. Unable to insert duplicate key into dbo.AspNetUsers object. Duplicate key value: (0e535b73-7cff-4152-9157-214b9821d264). The execution of this instruction has been interrupted.

I think that EF, update SearchFilter and ADD new user, which already exists, how can i avoid this?

Unnamed
  • 15
  • 8

1 Answers1

1

add UserId property and use it instead of reference User Property

public class SearchFilter : BaseEntity, IAggregateRoot
{
  public Guid UserId { get; set; }
  public virtual User User { get; set; }
}

then use insert or update

searchFilter.UserId = /* Add User Guid */;
SearchFilterRepository.Update(searchFilter);
Mohamed Adel
  • 492
  • 3
  • 8
  • Yes i know it, userid is a hidden property in a model, but i don't want see it in my model, I thought there is another solution – Unnamed Jan 13 '21 at 14:25
  • what do you mean by hidden property ? – Mohamed Adel Jan 13 '21 at 14:29
  • if I create only public virtual User User { get; set; } code first realization create UserId in DB table SearchFilter and i think have another solution that exclude create UserId property in a model. I have another model in which I do the same, an error falls, but the data is entered – Unnamed Jan 13 '21 at 14:40
  • perhaps you can somehow disable the addition of third-party entities from one entity – Unnamed Jan 13 '21 at 14:54
  • i think to update user you should select complete user object otherwise ef will consider this as new user so you can use userId when update or select full object of user to edit – Mohamed Adel Jan 13 '21 at 14:58
  • this also may help you https://stackoverflow.com/questions/41787327/how-can-i-reference-an-user-object-in-an-entity-framework-model – Mohamed Adel Jan 13 '21 at 15:00
  • Please tell me what it means to select a complete user object? I create a user and this user I send to my method, which uses it and assigns it SearchFilter – Unnamed Jan 13 '21 at 15:04
  • Sorry, but I don't understand what you mean, you can show me an example – Unnamed Jan 13 '21 at 16:00
  • Just select the entity from DB then assign this object, could you please share with me your fuction? And are you tried the solution i provided in my answer? – Mohamed Adel Jan 13 '21 at 16:28
  • This will help you https://www.learnentityframeworkcore.com/relationships/managing-one-to-many-relationships – Mohamed Adel Jan 13 '21 at 16:54
  • Your last answer helped me. Thank You. I do `_dbMainContext.Attach(user); filter.User = user; _dbMainContext.SaveChanges();` – Unnamed Jan 13 '21 at 17:28