Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在.net实体框架6中以编程方式确定实体类型_.net_Entity Framework - Fatal编程技术网

在.net实体框架6中以编程方式确定实体类型

在.net实体框架6中以编程方式确定实体类型,.net,entity-framework,.net,Entity Framework,我有一个用id更新特定记录的方法,并且有一些实体与该表具有相同的结构,所以我想知道我如何做下面的事情 public void deleteCarwithId(int id, int type) { string[] brands = { "BMW", "VOLKSWAGEN", "RENAULT", "FIAT" }; T entity = Convert.ToEntity(brands[type])

我有一个用id更新特定记录的方法,并且有一些实体与该表具有相同的结构,所以我想知道我如何做下面的事情

    public void deleteCarwithId(int id, int type)
            {
                string[] brands = { "BMW", "VOLKSWAGEN", "RENAULT", "FIAT" };
                T entity = Convert.ToEntity(brands[type]);//ofc there is no such a method yet
                var result =UnitOfWork.Repository<entity>().FirstOrDefault(u => u.ID == id);
                result.IsDeleted = true;
                Update(result);
            }
public void deleteCarwithId(int-id,int-type)
{
字符串[]品牌={“宝马”、“大众”、“雷诺”、“菲亚特”};
T entity=Convert.ToEntity(brands[type]);//ofc目前还没有这样的方法
var result=UnitOfWork.Repository().FirstOrDefault(u=>u.ID==ID);
result.IsDeleted=true;
更新(结果);
}
取而代之的是这个(它工作得很好,但很难看)

public void deleteCarwithId(int-id,int-type)
{
字符串[]品牌={“宝马”、“大众”、“雷诺”、“菲亚特”};
if(品牌[类型]=“宝马”)
{
var result=UnitOfWork.Repository().FirstOrDefault(u=>u.ID==ID);
result.IsDeleted=true;
更新(结果);
}
if(品牌[类型]=“大众”)
{
var result=UnitOfWork.Repository().FirstOrDefault(u=>u.ID==ID);
result.IsDeleted=true;
更新(结果);
}
}

实现这一点的最简单方法是使用反射和
MakeGenericMethod
函数:

第一:为将来要删除的类型创建接口:

public interface IDeletable
{
    bool IsDeleted { get; set; }
    int Id { get; set; }
}
第二,根据创建的接口结构分离逻辑功能

public void Delete<T>(UnitOfWork unitOfWork, int id) where T: IDeletable
{
    var repository = (Repository<T>)typeof(UnitOfWork).GetMethod("Repository")
        .MakeGenericMethod(typeof(T)).Invoke(new UnitOfWork(), new object[0]);
    var item = repository.FirstOrDefault(x => x.Id == id);
    item.IsDeleted = true;
    Update(item);
}
作为反射的替代方案,可以使用存储库字典: 假设上面的第一步已经完成,并且您的存储库是IQueryable,那么该方法看起来像:

public void deleteCarwithId(int id, int type)
{
    var source = new Dictionary<int, IQueryable<IDeletable>>
    {
        {0, unitOfWork.Repository<BMW>() }
        // all other types
    };
    var repository = source[i];
    var item = repository.FirstOrDefault(x => x.Id == id);
    item.IsDeleted = true;
    Update(item);
}
public void deleteCarwithId(int-id,int-type)
{
var source=新字典
{
{0,unitOfWork.Repository()}
//所有其他类型
};
var repository=source[i];
var item=repository.FirstOrDefault(x=>x.Id==Id);
item.IsDeleted=true;
更新(项目);
}

当然,为了减少不必要的初始化,可以将
字典创建为宿主对象中的字段,甚至是静态字段。如果是静态字段存储库,GC将永远不会收集存储库。

为什么要为不同的车型创建单独的实体?为什么汽车不只是实体上的一个属性?因为每辆汽车有太多的特定列。。。我希望能照你的建议去做,但在这种情况下不行
public void deleteCarwithId(int id, int type)
{
    var method = typeof(Program).GetMethod("Delete"); // Binding parameters could requires
    switch(type)
    {
        case 0: method.MakeGenericMethod(typeof(BMW))
                .Invoke(this, new object[]{unitOfWork, id});
                return;
        ...
    }
}
public void deleteCarwithId(int id, int type)
{
    var source = new Dictionary<int, IQueryable<IDeletable>>
    {
        {0, unitOfWork.Repository<BMW>() }
        // all other types
    };
    var repository = source[i];
    var item = repository.FirstOrDefault(x => x.Id == id);
    item.IsDeleted = true;
    Update(item);
}