0

I found multiple posts for this issue, however my issue is little different and error shown is exactly matches to my scenario.

  1. I have two different schema under by database. e.g. PreSchool.Students & School.Students. And per our requirements, rest is same for both Students tables, including primary key.

  2. Now in my code, I am updating record of 'PreSchool.Students' using following code:

    var wouldBeUpdatedStudent = await context.Students
                .FindAsync(student.StudentIdentity).ConfigureAwait(false);
    wouldBeUpdatedStudent = student; //updated student object
    context.Entry(wouldBeUpdatedStudent).State = EntityState.Modified; // This line throws error 
    var result = await context.SaveChangesAsync().ConfigureAwait(false);
    
  3. Under OnModelCreating I clearly set tableName & SchemaName for each of Entity. By this, I am expecting whenever I change entity/ model state, it should catch the correct table as per setting done under OnModelCreating. However this is not happening and throws exception.

I tried using context.Students.AsNoTracking() this works if no cascading to Students Entity. However, there are some cascading/children entities which are also same DDL. e.g. context.Student.Exams

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
Avi Kenjale
  • 2,414
  • 6
  • 29
  • 46
  • I asked a very similar question a couple years ago. Back then the answer was "Entity Framework doesn't support tables of the same name in different schemas in Sql Server." – Sam Axe Aug 15 '17 at 05:36
  • @SamAxe, fortunately, I tried [This answer](https://stackoverflow.com/a/31771363/659538) and worked for me. – Avi Kenjale Aug 15 '17 at 15:43

1 Answers1

0

Logically I didn't get answer which gives proper explanation -fix on this error. However This answer resolved my issue. One more think I observed with is solution is, if new object has exactly same values as per entity underlying database, it won't update table.

e.g.

context.Set<Student>().AddOrUpdate(wouldBeUpdatedStudent);
result = await context.SaveChangesAsync().ConfigureAwait(false);

Reason I cannot use AsNoTracking() because I have nested entities which are same across other schema. So Even if I use AsNoTracking() at top level entity, I get same error for child level. Same as I mentioned in my question.

Avi Kenjale
  • 2,414
  • 6
  • 29
  • 46