rivantsov / vita

VITA Application Framework
MIT License
59 stars 15 forks source link

ManyToMany appication crash #180

Closed rubenalves closed 3 years ago

rubenalves commented 3 years ago

I am already using Vita 3.1.1 and now when i insert a record try to save and call session.savechanges() i get no error but the aplication simply closes. it was ok in 1.9.x

i have this 👍

in one class: [ManyToMany(typeof(ILinhaSerial))] IList NumerosSerie { get; set; }

on other class: [ManyToMany(typeof(ILinhaSerial))] IList LinhasDocumento { get; set; }

the class ILinhaSerial [Entity] public interface ILinhaSerial { [PrimaryKey, Auto] Guid ID { get; set; }

    ILinhaDocumento LinhaDocumento { get; set; }

    INumeroSerie NumeroSerie { get; set; }
}

is there any way i can inspect what savechanges is doing and try to find the problem?

rivantsov commented 3 years ago

Can you run it under Visual Studio debugger? You can try to step into SaveChanges since I publish source packages, just enable this option in VS (option not My code Only). In general, this kind of crash should leave at least some hint in VS, in Output window, smth like "Access Violation", or "Stack overflow", when app has no way to recover with tracking/handling normal exception. These kind of errors usual come from mismatch of assembly versions, maybe Microsoft.Data.SqlClient, smth possibly related to your previous troubles. Try to experiment - add explicit reference to latest Microsoft.SqlClient package in your app and see what happens. Then Google on ways to investigate these kinds of crashes, using VS/windows debuggers/profilers. I assume all VITA tests in source code run OK? they include M2M case as well. Try to find what's different.

rubenalves commented 3 years ago

In visual studio debugger the debug just closes, no error, nothing.

I canot get visual studio to debug your code,

Looks like 1.9 will be my framework, i already lost 48 hours because of something that was working on older versions, so probably it is time to give up on 3.x Thanks for your time and continue the good work, but it is not for me.

rivantsov commented 3 years ago

hell no man, don't give up on this, that's just a hiccup; this is something to do with ADO.NET provider, just try referencing latest version of Microsoft.Data.SqlClient. Keep in mind, this trouble WILL show up with older version, as the OS, .NET, provider version and SQL move on, it will show up; you will have to deal with this. I am 99% sure it is NOT in VITA, but in setup of the app and assembly references mismatch. From your previous issue, when you were deleting config file - this messed up the startup and crashed it. I suspect smth like this is happening here, we need to fix it.
You'd better clean it up now, this startup trouble. As a last resort, I can just push a version with refreshed ref to latest SqlClient package. But it might be actually not this package, but smth it references.

rivantsov commented 3 years ago

one thing I see you do which I do not cover in tests is that you have a separate PK on link table, (not composite of two refs). I will check that setup if it works in tests

rubenalves commented 3 years ago

It has to be vita, every thing is working very well just when i insert a row in the many to many record it simply closes the debugger with no error, there where no changes to the tables.

rivantsov commented 3 years ago

it might be smth in VITA; interestingly, I was looking at similar crash in GraphQL, when no exc, nothing, the process just disappears. But it leaves couple of messages in the output:

image

note 'Access violation' error; and if I select Tests in top dropdown (output from), then I see another clue:

image

- stack overflow. So yes, it might be something in VITA, and that's the only way I see it might happen, when managed lib crashes the hosting app, so violently:

  1. Stack overflow (method calls itself recursively).
  2. Unmanaged code (SqlClient?) stepping out of memory bounds somewhere.

My first question is - do you see something like this in VS Output window when app crashes? like short message 'access violation'? that would help greatly. Another thing - can you still please try to reference Microsoft.Data.SqlClient package directly from your app and check if it helps? this will cover item 2, something wrong with provider.

I on my side will try to repro your case with many-2-many, the only diff I see is extra PK on link table, will see if it breaks things. It would be surprising, since other folks are using m2m, and do have separate PK (not composite key), and did not report any troubles. I will let you know if I find something shortly.

Just one thing to clarify. You say you spent 40+ hours on switching, it's too much and ready to give up. Well, the truth is that previous issues you discovered were not with new version, but with your app itself (data cache? crash in prod?). Isn't it good that you fixed these in your app, anyway? So let's not count these yet as part of the version switch problem, that's the first issue that has a chance of being new version problem. We'll figure it out, and make things run better all over the place.

rubenalves commented 3 years ago

I have it worting on 3.1 for over a wheek, i foud a problem with that many to may error and spent 40+ hours just trying to get passt that error, not the migration from one to another, that wa much more time but perfectly undestandable.

On my teste this past week it sometimes closes woth no error after login process, i restart debuging one or 2 times and it works but i did not think that it wold be related, but i have a copy of my app in 1.9 and it never happens after login, so they probably are related.

I get this error on the output window

Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Vita.Entities.DataAccessException' in Vita.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll Exception thrown: 'Microsoft.Data.SqlClient.SqlException' in Microsoft.Data.SqlClient.dll

Process is terminated due to StackOverflowException. The program '[1408] TsGest.exe' has exited with code -2147023895 (0x800703e9).

rubenalves commented 3 years ago

I have noticed that in nuget Vita.Data.MsSql depends on System.Data.SqlClient and not Microsoft.Data.SqlClient, it may be related with the problem.

rivantsov commented 3 years ago

damn that might be it, definitely a bug, although not 100% sure it will fix it, but needs to be fixed anyway. Will try to push update asap

rivantsov commented 3 years ago

fixed package ref, pushed sources and nuget packages, v 3.2.0 I also changed the link table in books project IBookAuthor to match your design, with an extra Id column as Primary key, instead of composite key based on two refs. All tests pass OK. Tried to look thru the code what might be happening there, can't find anything. Let me know if it fixes the issue. If not, and you cannot step into Vita's code, I would suggest to include VITA as sources, as projects in your solution, and run it and step into SaveChanges and see what's going on there.

rubenalves commented 3 years ago

I have updated Vita, now i get the exception

Violation of PRIMARY KEY constraint 'PK_Documento'. Cannot insert duplicate key in object 'dbo.Documentos'. The duplicate key value is (a84695fc-1ba6-431d-bec7-a5dec3da09b8). Violation of PRIMARY KEY constraint 'PK_LinhaDocumento'. Cannot insert duplicate key in object 'dbo.LinhasDocumento'. The duplicate key value is (fdce4ed3-5216-461c-bfd1-cfd25e3a1e9e). The INSERT statement conflicted with the FOREIGN KEY constraint "FK_LinhaSerial_NumeroSerie". The conflict occurred in database "OutraDB1", table "dbo.NumerosSerie", column 'ID'. The statement has been terminated. The statement has been terminated. The statement has been terminated.

Now i know where to find a solution.

Thanks.

rubenalves commented 3 years ago

Looks like vita is trying to insert the FK_LinhaSerial_NumeroSerie 2 times.

Any idea of how to make with compositive key as you mentioned?

thanks.

rivantsov commented 3 years ago

that wouldn't help I think, You clearly do the insertion of dbo.Documento twice with the same PK value. Make a breakpoint on session.SaveChanges, stop there, and inspect session.RecordsChanged inside EntitySession, these are records that will be inserted/updated, see if you can notice anything irregular

rubenalves commented 3 years ago

I canot find a solution yet,

I get a error The INSERT statement conflicted with the FOREIGN KEY constraint "FK_LinhaDocSerial_NumerosSerie". The conflict occurred in database "NOVASB2", table "dbo.NumerosSerie", column 'ID'. The statement has been terminated.

I have tryed to make change my wntity like this [Entity, PrimaryKey("LinhaDocumento,NumeroSerie")] public interface ILinhaDocSerial { //[PrimaryKey, Auto] //Guid ID { get; set; }

    ILinhaDocumento LinhaDocumento { get; set; }

    INumeroSerie NumeroSerie { get; set; }
}

but no luke with it

do you have any idea of what it can be? Thanks.

rivantsov commented 3 years ago

the error says that the value of NumeroSerie_Id column in link table is wrong, most likely it is empty. Make a stop before SaveChanges, inspect session.RecordsChanged list, find the link table record, look at its column values (in record, look at ValuesModified array). Another thing to check, when you add an entity to M2M list property, check it's contents and that its ID column is assigned. But the main check is - before the save changes, find the link record in session.RecordsChanged and see what are the values

rubenalves commented 3 years ago

I have inspected and found the problem, i was creating a new session for the main record and and a new session form the serialnumber with the many to many, it was because of some refactoring o made and that one was forgoten. Sorry because that error was on my side but on the bright side o found a error :) Thank you very much for your time and the speed you resolve the issues on the framework, now i am a very happy man :) Your the man,

rivantsov commented 3 years ago

Hooray! congrats, no problem, always here to help. Thank you for using VITA! (and thank you for finding the package ref issue, sorry about that)