entity framework help needed pls.
So I have this method which is the main logic of Spinning mechanism.
And well those are the main models where I have issue, Issue is that when I assign dateEnded to session which means that this certain session is ended right, in this spin logic in one of t he first statement it checks that, if the session is ended it creates new session which I want to be saved in the database. and I am 100% sure I assign playerId to it but well the error says it can't be assigned the null and doesn't saves Here is part of debugging pics too and it txt file there is spin method.
86 Replies
db context
player session
In the first Image you can clearly see that PlayerId is assigned - 1
however error says otherwise
It could be that its' referencing to different thing but I dont know .... I tried saving new session first but when I save it errors on that save now
:(
If anyone needs me to provide anything else please tell me I wanna fix this asap
If anyone responds pls ping me
there is no csproj but i imagine you have nullable enabled
oh yeas
I have nullable enabled
that could be the problem?
hmm im still reading tbh but considering field is nullable and a struct i guess it's not a problem
yeah didn't change a thing when I disabled.
This is the InnerException btw : SqlException: Cannot insert the value NULL into column 'PlayerId', table 'DeepDiveSlots.dbo.PlayerSessions'; column does not allow nulls. UPDATE fails.
pardon, i was looking at the end date and not playerId
which btw why is a string
well it will take guid most likely
that's why
im assuming I have foreign keys and relations messed up
technically this
could also be
since there's already the variable
but did you debug this step by step? did you see if fields are filled correctly?
well true but either way it still takes same id
yes I did
if you check the first image
which is taken in debugging mode
all the non-nullables are filled
Especially that "PlayerId" is assigned to player too
in theory yes, but in practice code is not working so you can't be sure
do you have the generated sql in the logs?
is it really null?
I don't have logs yet
how can I do that tho?
okay wait I will check sql query string now
eh but you need the update query, not the select one

This is where it errors
yeah but doesn't ef generate that itself?
the driver writes the query but maybe this is erroring before getting there i don't know
doesn't seem like an sql server exception
no it should be from the server, weird
I also get this error:
just to be sure, is your db in sync with the models?
migration u mean?
yes
wait let me check it must be tho
yeah it must be
I added another migration and updated db just in case
still same error
(ps i would use
nameof()
instead of magic string in attributes like [ForeignKey("Player")]
)yeah thanks for reminding that, this is newly started project so when I write simple logic I then rewrite it more optimized
anyway this is kinda weird, i would still try to recover insert/update sql to look if [session]id/playerId are what they should be
in that case
before adding EndSession and starting new session logic
It has saved last session in database.
so logically when I run the code player still have old session attached but its' isActive is false that's why it goes inside that if else statement and tries to attach new playerSession to it
but in old session data playerId and everything is fine

Player HasOne playerSession is right logic too player can't be playing two different sessions so I don't think anything is wrong with model builder either
should I clean project or something?
oh wait
it's dinner time for me but
this i bet could be a return value instead of an exception
and
you shouldn't be assigning
Player
here
only playerId
oh
yeah logical
but I think I had that way before error too haha
wait lemme test
thanks for trying to help tho
however this wasn't solution to it
can you re-$paste your code to the site below... discord is awful to read when it embeds it
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
yeah sure wait
you can create tabs at the top to separate pages and do it all in link
also what db engine are u using sql? sqlite? postgresql? something else?
BlazeBin - wjhqvmrzncmz
A tool for sharing your source code with the world!
SQL
oh wait I didnt read about tabs
MS SQL I meant to say
wait I will seperate
yeah there is a + at the top
you can add multiple "files" per say
yes
BlazeBin - xdzjaajpyybz
A tool for sharing your source code with the world!
here
with tabs
so first thing I see wrong is
in SpinAsync Method where I get the error you can see lots of comments where I try lots of different things, saving stuff before stuff and etc.
its not the code's responsibility to generate the ID AFAIK its handled by the database
Yeah I know that I don't handle creating/updating/deleting players currently tho
so just
or
either will work u dont need to specify Key either
its implied by efc
Okay I changed that
you also dont need to define these in both code and fluent api
its also implied
[ForeignKey(nameof(Player))]
Soo that is unnecessary on top?
ideally you just need to do for example
and efc takes care of the relationship for u under the hood
okay
let me look where was the error
moment
on saveChangesAsync() before that I make new session and I want it to be added to db
in the image I can't really see the code it breaks or line
let me look sec
so line 30
on the first tab
you could just do
in that case I have to give
playersession constructor
wait
oh no
wait
all that code becomes just this
it might be a problem is that player had a session already thou
even if not active
yeah that's the thing that
player has the session
so u would need a separate condition to handle it if session is not nul
Btw I tried to assign null to that, thats why I made playerSession nullable in first place
and change it appropriately and save
but it gave me exactly same error as this
you can't just assign null to it
you either delete the session attached to that player and create a new one
or u update the one it already has
yeah so update
because I want sessions to be saved in table
but honestly to me it feels like a player could potentially have many sessions
exception would be active vs terminated sessions?
so one to many instead
yeah
so basically active session is the one that player is currently playing
so yeah that would be a one to many instead
and other sessions which already has an EndDate which means it's not active anymore
as you have right now u would only have 1 entry for session per player
HasOne to HasMany
on model builder or just make ICollection
no need to do anything on model builder
you can refer to https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#mapping-relationships-in-ef-core
and what happens here is that u just pulled player from the db, and its being tracked
so any changes you do will be reflected if u call savechanges
so I dont need to create session and gamestate separately to save each
wait does List work in ef? I was used to making ICollections
I can just create then the way I am doing and save changes will take care of the rest
you can use either
do you also mean to remove ActiveSession?
and just leave it to Sessions and the one with no EndDate in code logic will be ActiveSession?
ideally u would use IReadOnlyCollection or ICollection
and also should I just remove my modelBuilder code?
ICollection was often used for lazy loading which is not recommended unless u have a very specific scenario
well u would query for the active session now
ideally we dont use models directly we project the results with only the data we need to DTOs to avoid certain issues as well as separate concerns
sure
Yeah I know about DTos I didn't use it in this project it would make stuff easier tbh
ok
it's just I straight away started working on logic and then try to optimize the code itself
which will take a LONG time now I look at it
its not really optmization but ok 😉
yeah I know it is not xd
u should've seen before
and in this case
now we just add another session
sure
okay I am switching up a lots of code now and I'll try if that works
thanks for helping