// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
namespace System.Data.Entity.Infrastructure
{
using System.Data.Entity.Internal;
using System.Data.Entity.Resources;
using System.Data.Entity.Utilities;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
///
/// A non-generic version of the class.
///
public class DbReferenceEntry : DbMemberEntry
{
#region Fields and constructors
private readonly InternalReferenceEntry _internalReferenceEntry;
///
/// Creates a from information in the given .
/// Use this method in preference to the constructor since it may potentially create a subclass depending on
/// the type of member represented by the InternalCollectionEntry instance.
///
/// The internal reference entry.
/// The new entry.
internal static DbReferenceEntry Create(InternalReferenceEntry internalReferenceEntry)
{
DebugCheck.NotNull(internalReferenceEntry);
return (DbReferenceEntry)internalReferenceEntry.CreateDbMemberEntry();
}
///
/// Initializes a new instance of the class.
///
/// The internal entry.
internal DbReferenceEntry(InternalReferenceEntry internalReferenceEntry)
{
DebugCheck.NotNull(internalReferenceEntry);
_internalReferenceEntry = internalReferenceEntry;
}
#endregion
#region Name
///
/// Gets the property name.
///
/// The property name.
public override string Name
{
get { return _internalReferenceEntry.Name; }
}
#endregion
#region Current values
///
/// Gets or sets the current value of the navigation property. The current value is
/// the entity that the navigation property references.
///
/// The current value.
public override object CurrentValue
{
get { return _internalReferenceEntry.CurrentValue; }
set { _internalReferenceEntry.CurrentValue = value; }
}
#endregion
#region Loading
///
/// Loads the entity from the database.
/// Note that if the entity already exists in the context, then it will not overwritten with values from the database.
///
public void Load()
{
_internalReferenceEntry.Load();
}
#if !NET40
///
/// An asynchronous version of Load, which loads the entity from the database.
/// Note that if the entity already exists in the context, then it will not overwritten with values from the database.
///
/// A Task representing the asynchronous operation.
public Task LoadAsync()
{
return LoadAsync(CancellationToken.None);
}
///
/// An asynchronous version of Load, which loads the entity from the database.
/// Note that if the entity already exists in the context, then it will not overwritten with values from the database.
///
/// The token to monitor for cancellation requests.
/// A Task representing the asynchronous operation.
public Task LoadAsync(CancellationToken cancellationToken)
{
return _internalReferenceEntry.LoadAsync(cancellationToken);
}
#endif
///
/// Gets a value indicating whether the entity has been loaded from the database.
///
///
/// true if the entity is loaded; otherwise, false .
///
public bool IsLoaded
{
get { return _internalReferenceEntry.IsLoaded; }
}
#endregion
#region Query
///
/// Returns the query that would be used to load this entity from the database.
/// The returned query can be modified using LINQ to perform filtering or operations in the database.
///
/// A query for the entity.
public IQueryable Query()
{
return _internalReferenceEntry.Query();
}
#endregion
#region Back references
///
/// The to which this navigation property belongs.
///
/// An entry for the entity that owns this navigation property.
public override DbEntityEntry EntityEntry
{
get { return new DbEntityEntry(_internalReferenceEntry.InternalEntityEntry); }
}
#endregion
#region InternalMemberEntry access
///
/// Gets the backing this object as an .
///
/// The internal member entry.
internal override InternalMemberEntry InternalMemberEntry
{
get { return _internalReferenceEntry; }
}
#endregion
#region Conversion to generic
///
/// Returns the equivalent generic object.
///
/// The type of entity on which the member is declared.
/// The type of the property.
/// The equivalent generic object.
public new DbReferenceEntry Cast() where TEntity : class
{
var metadata = _internalReferenceEntry.EntryMetadata;
if (!typeof(TEntity).IsAssignableFrom(metadata.DeclaringType)
|| !typeof(TProperty).IsAssignableFrom(metadata.ElementType))
{
throw Error.DbMember_BadTypeForCast(
typeof(DbReferenceEntry).Name,
typeof(TEntity).Name,
typeof(TProperty).Name,
metadata.DeclaringType.Name,
metadata.MemberType.Name);
}
return DbReferenceEntry.Create(_internalReferenceEntry);
}
#endregion
}
}