C
C#β€’3y ago
esther

Error in AsNoTracking()

I don't know why it show this error. NOMOR is string and unique value.
35 Replies
esther
estherOPβ€’3y ago
and this is my code
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR.Equals(nomor)).Count()
.AsNoTracking().FirstOrDefault();
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR.Equals(nomor)).Count()
.AsNoTracking().FirstOrDefault();
Saber
Saberβ€’3y ago
.Count() is getting the count based on your condition, it is no longer a iqueryable, it is an int
esther
estherOPβ€’3y ago
Based on condition what is mean? i used count to get data for NOMOR since NOMOR is unique value i thought this right using count πŸ˜… i still confusing why is it no longer iqueryable is it because the "count"? sorry i'm still learning
Saber
Saberβ€’3y ago
Count literally gets the count. So doing anything else linq related after it doesn't make sense
esther
estherOPβ€’3y ago
Okey i get it, then i was trying to make it like this one but my 'e' error too😡
esther
estherOPβ€’3y ago
@Kouhai can you help me? I'm sorry for pinging you πŸ‘‰ πŸ‘ˆ
Kouhai
Kouhaiβ€’3y ago
No problem blobthumbsup what's the issue?
esther
estherOPβ€’3y ago
I want to try get data by nomor since NOMOR is unique value i try to use count or distant but didn't work
Kouhai
Kouhaiβ€’3y ago
Okay, both Count and Distinct work differently that what you probably expect. You could do
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
esther
estherOPβ€’3y ago
I tried this before but it give me data not found
Kouhai
Kouhaiβ€’3y ago
LaporanPenilaian.NOMOR is a string, right?
esther
estherOPβ€’3y ago
yes string
Kouhai
Kouhaiβ€’3y ago
Alright, try running FindByNomor with a value you're 100% sure it exists, does return null?
esther
estherOPβ€’3y ago
This is actually my controller. Yes it's null
esther
estherOPβ€’3y ago
Kouhai
Kouhaiβ€’3y ago
Just to be sure, the current implementation is this?
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
esther
estherOPβ€’3y ago
yes kou i use this
Kouhai
Kouhaiβ€’3y ago
How about this
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>().FirstOrDefault();
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>().FirstOrDefault();
This won't do any comparisons, but just for a quick test
esther
estherOPβ€’3y ago
This workπŸ˜…
Kouhai
Kouhaiβ€’3y ago
Great, what value did it return?
esther
estherOPβ€’3y ago
It gives return the data
Kouhai
Kouhaiβ€’3y ago
Oops I just realized NOMOR is being encoded
esther
estherOPβ€’3y ago
but i tried to input wrong nomor after it return the same result
Pobiega
Pobiegaβ€’3y ago
To answer the actual question here: just call AsNoTracking on the set before you do anything else before the where, before the count etc
Kouhai
Kouhaiβ€’3y ago
Okay, use this again
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
public LaporanPenilaian FindByNomor(string nomor) => MappiEgovContext.Set<LaporanPenilaian>()
.Where(e => e.NOMOR == nomor)
.AsNoTracking().FirstOrDefault();
And make sure to call it FindByNomor(Uri.UnescapeDataString(NOMOR))
esther
estherOPβ€’3y ago
Thanks it catlove I don't know this > And make sure to call it FindByNomor(Uri.UnescapeDataString(NOMOR)) what's this actually?
Kouhai
Kouhaiβ€’3y ago
NOMOR is being url encoded, basically you have characters like / which is used as a separator in URLs, that character when passed as a query parameter for instance get's encoded to %2F by default it's not decoded back to /
Kouhai
Kouhaiβ€’3y ago
If you look at your input
Kouhai
Kouhaiβ€’3y ago
And how it's actually being requested
Kouhai
Kouhaiβ€’3y ago
Kouhai
Kouhaiβ€’3y ago
You'll see that / became %2F Uri.UnescapeDataString just decodes the string back to it's original format
esther
estherOPβ€’3y ago
OMG Kuo, you gave me an answer that i was looking for before, was i trying to find how to decode this cause NOMOR was unique code. Thank you so much😊
Kouhai
Kouhaiβ€’3y ago
No problem blobthumbsup
esther
estherOPβ€’3y ago
kuo i want to ask, i just don't understand why, i tried on local about this ok, i got the data, but when i tried on server production error.
Kouhai
Kouhaiβ€’3y ago
Are there any exceptions?
Want results from more Discord servers?
Add your server