Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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
C# 公共reposult创建(T项){}获取/读取项的主键值_C#_Entity Framework 6_Asp.net Mvc 5.2_.net 4.5.2 - Fatal编程技术网

C# 公共reposult创建(T项){}获取/读取项的主键值

C# 公共reposult创建(T项){}获取/读取项的主键值,c#,entity-framework-6,asp.net-mvc-5.2,.net-4.5.2,C#,Entity Framework 6,Asp.net Mvc 5.2,.net 4.5.2,我试图建立一个通用回购协议,但遇到了一些问题。我有以下界面: public interface IPKProvider { bool PKHasNoValue(); } 公共接口IGenRepo,其中T:class { IQueryable项{get;} T-find(TKey-pk); reposult删除(TKey pk); 报告结果创建(T项); 报告结果更新(T项); reposult save(); } 下面是实现该接口的类: public interface IPKPro

我试图建立一个通用回购协议,但遇到了一些问题。我有以下界面:

public interface IPKProvider
{
    bool PKHasNoValue();
}
公共接口IGenRepo,其中T:class
{
IQueryable项{get;}
T-find(TKey-pk);
reposult删除(TKey pk);
报告结果创建(T项);
报告结果更新(T项);
reposult save();
}
下面是实现该接口的类:

public interface IPKProvider
{
    bool PKHasNoValue();
}
公共类EFGenRepo:IGenRepo其中T:class
{
私有PortalEntities上下文=新PortalEntities();
公共IQueryable项{get{return context.Set().AsQueryable();}}
公共T-find(TKey-pk){}
公共reposult delete(TKey pk){}
公共报告结果创建(T项){
if(item==null)返回reposult.Failed(reposessages.paramIsNull());
if(true){//请在此提供帮助,类似于:if(item.PKHasNoValue==true)
context.Set().Add(项);
返回save();
}否则{
返回reposult.Failed(RepoMessages.PKIsNotZeroAtCreate());
}
}
公共报告结果更新(T项){}
公共结果保存(){
试一试{
SaveChanges();
返回新结果();
}捕获(例外e){
返回reposult.Failed(RepoMessages.savechangesfaile(e));
}
}
私有reposult保存(T项){}
}
在我的create方法中,我想检查传递给该方法的项的PK值。我知道我可以在调用代码之前在我的控制器中检查这一点,但我想这样做!假设我有一个学生班,如下所示:

公立部分班学生
{       
公共int id{get;set;}
公共字符串naam{get;set;}
}
作为id为0的学生对象,现在我希望if语句的计算结果为true。如果学生对象已具有id,则其计算结果必须为false。但是我怎么能检查这个呢。我想我必须使用下面的代码,但我没有这方面的技能:

typeof(TKey).Name;
item.GetType().Name;

非常感谢您的帮助

我提出的解决方案是创建一个接口:

public interface IPKProvider
{
    bool PKHasNoValue();
}
我让所有自动生成的EF类(学生只是一个例子)实现这个接口(在一个单独的部分类文件中)

因此,单独的学生班级如下所示:

另一个EF实体,比如说,用户将如下所示:

现在,我可以在EFGenRepo类中实现IPKProvider接口,如下所示:

公共类EFGenRepo:IGenRepo其中T:class,IPKProvider
{
私有PortalEntities上下文=新PortalEntities();
公共IQueryable项{get{return context.Set().AsQueryable();}}
公共T-find(TKey-pk){}
公共reposult delete(TKey pk){}
公共报告结果创建(T项){
if(item==null)返回reposult.Failed(reposessages.paramIsNull());
if(item.PKHasNoValue()){//
public class EFGenRepo<T, TKey> : IGenRepo<T, TKey> where T : class, IPKProvider
{
    private PortalEntities context = new PortalEntities();

    public IQueryable<T> Items { get { return context.Set<T>().AsQueryable<T>(); } }

    public T find(TKey pk){}

    public RepoResult delete(TKey pk){}

    public RepoResult create(T item){
        if (item == null) return RepoResult.Failed(RepoMessages.paramIsNull());
        if (item.PKHasNoValue()) {//<----------PROBLEM SOLVED
            context.Set<T>().Add(item);
            return save();
        } else {
            return RepoResult.Failed(RepoMessages.PKIsNotZeroAtCreate());
        }
    }
    public RepoResult update(T item){}

    public RepoResult save(){
        try {
            context.SaveChanges();
            return new RepoResult();
        } catch (Exception e){
            return RepoResult.Failed(RepoMessages.saveChangesFailure(e));
        }
    }

    private RepoResult save(T item){}
}