c# - NullReferenceException on object initialized with FormatterServices.GetUninitializedObject -
I'm joking a DbContext, and in my tests, I want to make sure institutions will be marked for extinction. As I have marked each object in my laughing context, I track each unit with a static dictionary:
public DbEntityEntry > FormatterServices.GetUninitializedObject (typef (DbEntityEntry & lt; T & gt;)); Var state = existence [unit]; Entry. State = state; Return entry; } Public Zero SetEntityState (Object unit, System.Data.EntityState state) {entityStates.Add (unit, state); Entry (unit) .set = position; } Private Stable Dictionary & lt; Object, Entitestate & gt; Unit Location = New Dictionary & lt; Object, Entitestate & gt; ();
I did not get instantiated with DBTTinTree
Activator.CreateInstance ()
, so I have instantiated with FormatterServices .GetUninitializedObject ()
.
The problem is this line:
entry.State = state;
In trying to assign results of this property:
NullReferenceException: object reference is not set to an instance of an object
Obviously the name of the function has not been started in the object, but I am unsure why I can not specify the properties of the object without throwing this exception.
FWI, Test looks something like this:
TripManager. Delay Trips Bayregginki (Int Carid); Var trip1 = _fakeDbContext.Trips.FirstOrDefault (t => T. TRPID == 1); Var Access = _fakeDbContext.Entry (trip1); Confirm. Erequal (EntityState.Deleted, entry.State);
formater services. Gate unified object
does not call any constructor. This means that the fields in which the default values are or set in the constructor are not set. If you look at the definition of the state
property (- forgive the mono version), then you can see that the setter depends on _internalEntityEntry
, which Constructor:
Internal DbEntityEntry (Internal Interval EntryInterientEntityEntry) {_internalEntityEntry = internalEntityEntry; }
Of course, why should not you wander around with the formater services? GetUnited Object
:)
If you really know what you are doing, you can explicitly call the internal constructor and also with InternalEntityEntry
Can mock ... but it really feels like a bad idea.
Comments
Post a Comment