I am new to the world of EF. Made a connection to the database on the principle of Database First. I need to find all occurrences of "ul" in the field of the School. Address and replace with "st." I kind of did it:

var context = new SchoolEntities(); foreach (var school in context.Школы) { school.Адрес = school.Адрес.Replace("ул ", "ул."); } context.SaveChanges(); 

But this error appears:

 Исключение типа "System.Data.Entity.Infrastructure.DbUpdateException" возникло в EntityFramework.dll, но не было обработано в коде пользователя Дополнительные сведения: Unable to update the EntitySet 'Школы' because it has a DefiningQuery and no <UpdateFunction> element exists in the <ModificationFunctionMapping> element to support the current operation. 
  • one
    school.Адрес is a string? Strings in C # are immutable. - VladD
  • one
    This question should be closed, because the author went missing 2 years ago :) - Pavel Mayorov
  • @PavelMayorov eeee, don't do that. I'm here). - Adam
  • ... you want to say that the problem has not been solved yet?) - Pavel Mayorov
  • because you changed the question several times - there is no right answer. - Pavel Mayorov

3 answers 3

instead

school.Address.Replace ("st", "st.");

write

school.Address = school.Address.Replace ("st", "st.");

and don't forget to save changes in context

    context.SaveChanges ();

    • Did as requested and jumped: Unable to update the EntitySet 'Schools' because it has the same function as the update operation. - Adam
    • @derkode: So it turns out that Школы are not a table, but a view? - VladD
    • No, this is a table in the database. On stackoverflow, I found something like the answer: stackoverflow.com/questions/7583770 / ... It seems to say that this is due to the fact that I do not have a primary key in the table - Adam

    Try this code here:

     foreach (var school in context.Школы) { school.Адрес = school.Адрес.Replace("ул ", "ул."); context.SaveChanges(); }