簡體   English   中英

實體框架4:如何找到主鍵?

[英]Entity Framework 4: How to find the primary key?

我正在嘗試使用EF4創建一個通用方法來查找對象的主鍵。

public string GetPrimaryKey<T>()
{
    ...
}

為了提供更多信息,我正在使用Tekpub StarterKit,下面是我試圖啟動和運行的類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data.Objects.ELinq;
using System.Data.Linq;
using Web.Infrastructure.Storage.EF4;

namespace Web.Infrastructure.Storage {
public class EFSession:ISession {
    PuzzleEntities _db;//This is an ObjectContext
    public EFSession() {
        _db = new PuzzleEntities();
    }

    public void CommitChanges() {
        _db.SaveChanges();
    }
    /// <summary>
    /// Gets the table provided by the type T and returns for querying
    /// </summary>
    private ObjectSet<T> GetObjectSet<T>() where T:class {
        return _db.CreateObjectSet<T>();
    }

    private T GetByPrimaryKey<T>() where T: class
    {
        .....
    }

    public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T: class{

        foreach (T item in All<T>().Where(expression))
        {
            GetObjectSet<T>().DeleteObject(item);
        }
    }

    public void Delete<T>(T item) where T : class {
        GetObjectSet<T>().DeleteObject(item);
    }

    public void DeleteAll<T>() where T : class {
        foreach(T item in All<T>())
        {
            GetObjectSet<T>().DeleteObject(item);
        }
    }

    public void Dispose() {
        _db.Dispose();
    }

    public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T:class {
        return GetObjectSet<T>().SingleOrDefault(expression);
    }

    public IQueryable<T> All<T>() where T : class {
        return GetObjectSet<T>().AsQueryable();
    }

    public void Add<T>(T item) where T : class {
        GetObjectSet<T>().AddObject(item);
    }
    public void Add<T>(IEnumerable<T> items) where T : class {
        foreach (T item in items)
        {
            GetObjectSet<T>().AddObject(item);
        }
    }
    public void Update<T>(T item) where T : class {
        //nothing needed here
    }
}
}

所以我終於能夠找到如何讓它發揮作用。 我希望我沒有丟失我昨晚讀到的博客的鏈接,因為我沒有寫代碼。

public T GetByPrimaryKey<T>(int id) where T : class
{
    return (T)_db.GetObjectByKey(new EntityKey(_db.DefaultContainerName + "." + this.GetEntityName<T>(), GetPrimaryKeyInfo<T>().Name, id));
}

string GetEntityName<T>()
{
    string name = typeof(T).Name;
    if (name.ToLower() == "person")
        return "People";
    else if (name.Substring(name.Length - 1, 1).ToLower() == "y")
        return name.Remove(name.Length - 1, 1) + "ies";
    else if (name.Substring(name.Length - 1, 1).ToLower() == "s")
        return name + "es";
    else
        return name + "s";
}

private PropertyInfo GetPrimaryKeyInfo<T>()
{
    PropertyInfo[] properties = typeof(T).GetProperties();
    foreach (PropertyInfo pI in properties)
    {
        System.Object[] attributes = pI.GetCustomAttributes(true);
        foreach (object attribute in attributes)
        {
            if (attribute is EdmScalarPropertyAttribute)
            {
                if ((attribute as EdmScalarPropertyAttribute).EntityKeyProperty == true)
                    return pI;
            }
            else if (attribute is ColumnAttribute)
            {

                if ((attribute as ColumnAttribute).IsPrimaryKey == true)
                    return pI;
            }
        }
    }
    return null;
}

我希望這有助於其他人。 我只能說,如何做到這一點應該更清楚一些。

每個名為EntityKey EF4實體都有一個屬性,它包含一個EntityKeyValues數組(如果是復合鍵,則為數組)。

您可以直接在您的實體實例上引用它,或者創建一個通用的幫助方法來完成此操作。 如果我可以測試一些示例代碼,我會在這里發布。

編輯 :EntityKeyValue是KeyValuePair<TKey, TValue> ,其中key是實體的主鍵字段, value是關聯值。

例如,我有一個名為Company的實體,其主鍵是字段Symbol

var firstCompany = (from c in context.Companies select c).FirstOrDefault();
var kvp = firstCompany.EntityKey.EntityKeyValues[0];
// kvp shows {[Symbol, FOO]}

在我的沙箱中,我注意到當我在代碼中創建實體時,此屬性為null 但是一旦我從數據庫中讀取實體,它就被正確填充了。 因此,看起來主鍵的EF4概念只有在它到達數據庫時才能發揮作用。 雖然如果您願意,可以提前明確地設置它。

我假設很多人只是通過查看“實體框架如何找到主鍵?”來阻止這篇文章。 無論EF版本(像我一樣)。 所以我想提一下,使用EF 6.1,您還可以創建擴展方法來獲取主鍵。 以下是示例,並且完美地運行。

PS:我不是百分百肯定,如果那可以使用復合鍵和復合鍵。

using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;

namespace System.Data.Entity
{
    public static class DbContextExtensions
    {
        public static string[] GetKeyNames<TEntity>(this DbContext context)
            where TEntity : class
        {
            return context.GetKeyNames(typeof(TEntity));
        }

        public static string[] GetKeyNames(this DbContext context, Type entityType)
        {
            var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace;

            // Get the mapping between CLR types and metadata OSpace
            var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace));

            // Get metadata for given CLR type
            var entityMetadata = metadata
                    .GetItems<EntityType>(DataSpace.OSpace)
                    .Single(e => objectItemCollection.GetClrType(e) == entityType);

            return entityMetadata.KeyProperties.Select(p => p.Name).ToArray();
        }
    }
}

原始來源

這似乎是不必要的長? 我有同樣的需求,並使用上面的建議(由SethO和denis_n),我正在使用:

        //get the primary key field name and location for the table
        var primaryFieldName = entry.EntitySet.ElementType.KeyMembers[0].Name ;
        int primaryFieldLocation = entry.CurrentValues.GetOrdinal(primaryFieldName);
        //gets the value pair for the primary key (returns field name + value)
        var primaryField = entry.EntityKey.EntityKeyValues[primaryFieldLocation];
        String primaryFieldValue = primaryField.Value.ToString();

希望這有助於任何有興趣的人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM