如何使用CreateObjectSet <tentity>将TEntity作为参数?</tentity>

时间:2011-12-08 07:44:51

标签: c# entity-framework

我正在寻找一种将Entity和TEntity作为参数传递的方法,以便我可以将此方法用作通用方法。这样的事情:

    private void AttachSingleEntity(Entity singleEntity, TEntity Tentity)
    {       
        ObjectSet<Tentity> objectSet = context.CreateObjectSet<Tentity>();
        objectSet.Attach(singleEntity);
    }

然后我调用AttachSingleEntity如下:

    customer cus = new customer {id = 1};
    AttachSingleEntity(cus,customer);

1 个答案:

答案 0 :(得分:4)

更改为

private void AttachSingleEntity<TEntity>(Entity singleEntity) where TEntity:class
{       
    ObjectSet<TEntity> objectSet = context.CreateObjectSet<TEntity>();
    objectSet.Attach(singleEntity);
}