实体框架4如何查找主键? [英] Entity Framework 4 How to find the primary key?

查看:173
本文介绍了实体框架4如何查找主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用EF4找到对象的主键的通用方法。

I am trying to create a generic method using EF4 to find the primary key of an object.

例如

公共字符串的getPrimaryKey() { .... }

public string GetPrimaryKey() { .... }

要给予更多的信息我工作过的Tekpub入门套件,并在下面的类我试图启动和运行

To Give more info I am working off of the Tekpub StarterKit and below is the class I am trying to get up and running

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
    }

}

}

推荐答案

所以,我终于能够找出如何得到这个工作。我想我并没有失去链接到博客我看了昨天晚上,当我没写code。

So I was finally able to find out how to get this to work. I wish I hadn't lost the link to the blog I read last night as I didn't write the code.

       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;
    }

我希望这可以帮助别人。所有我能说的是,它应该是一个小更清楚如何做到这一点。

I hope this helps someone else. All I can say is that it should be a little clearer on how to do this.

这篇关于实体框架4如何查找主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆