IsKeySet: Added
The EntityEntry object, which holds the tracking information for each entity, has a new property called IsKeySet. IsKeySet is a great addition to the API. It checks to see if the key property in the entity has a value. This eliminates the guessing game (and related code) to see if an object already has a value in its key property (or properties if you have a composed key). IsKeySet checks to see if the value is the default value of the particular type you specified for the key property. So if it’s an int, is it 0? If it’s a Guid, is it equal to Guid.Empty (00000000-0000-0000-0000-000000000000)? If the value is not the default for the type, IsKeySet returns true.
If you know that in your system you can unequivocally differentiate a new object from a pre-existing object by the value of its key property, then IsKeySet is a really handy property for determining the state of entities.
public class MyClass
{
public DateTime? DateExample { get; set; }
}
在你的编程逻辑中使用:
DateTime? dateExample = null;
if (!dateExample.HasValue)
{
Console.WriteLine("Is Null"); // Is Null
}
dateExample = DateTime.Now;
if (dateExample.HasValue)
{
Console.WriteLine("Is Not Null"); // Is Not Null
}