/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects.DataClasses;
namespace Everest.Library.Data.Entity{
public static class EntityReferenceExtension
{
/// <summary>
/// Loads the specified reference.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reference">The reference.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static T Load<T>(this EntityReference<T> reference, T value, EntityState state)
where T : class, System.Data.Objects.DataClasses.IEntityWithRelationships
{
if (value == null && !reference.IsLoaded && state != EntityState.Added && state != EntityState.Detached)
{
reference.Load();
value = reference.Value;
}
return value;
}
/// <summary>
/// Gets the key value.
/// </summary>
/// <typeparam name="ValueType">The type of the alue type.</typeparam>
/// <param name="entityKey">The entity key.</param>
/// <param name="keyName">Name of the key.</param>
/// <returns></returns>
public static ValueType GetKeyValue<ValueType>(this EntityKey entityKey, string keyName)
{
if (entityKey == null)
{
return default(ValueType);
}
return entityKey.EntityKeyValues.Where(k => k.Key == keyName && k.Value != null).Select(k => (ValueType)k.Value).FirstOrDefault();
}
}
}
|