C# 具有自定义列名的C MVC通用存储库

C# 具有自定义列名的C MVC通用存储库,c#,model-view-controller,generics,repository,design-patterns,C#,Model View Controller,Generics,Repository,Design Patterns,我如何才能实现以下目标 public interface IGenericRepository { int id { get; } T GetById<T>() where T : class } public class GenericRepository : IGenericRepository { //Some code here public T GetById<T>(int tid) where T : class

我如何才能实现以下目标

public interface IGenericRepository 
{
    int id { get; }

    T GetById<T>() where T : class
}

public class GenericRepository : IGenericRepository 
{
    //Some code here

    public T GetById<T>(int tid) where T : class 
    { 
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
 }
除此之外,别忘了在模型中的某个地方定义:

public partial class Category : IHasId { }
用法是:

Repository rep = new Repository();
Category cat = rep.GetById<Category>(15);

之所以出现此错误,是因为您接受T:class所在的每个类。类没有该属性


创建一个抽象类或接口以确保此属性存在,并将where T:class更改为where T:IHasIdProperty。

您会收到此错误,因为您接受了where T:class的每个类。类没有该属性


创建一个抽象类或接口,以确保此属性存在,并将where T:class更改为where T:IHasIdProperty。

这里有几个问题-第一个问题是您要匹配的泛型类型是一个类,但一个类没有名为“id”的属性。您需要让Category类实现一个公开“id”属性的接口:

public interface IIdentity
{
    int identity { get; set; }
}

public class Category : IIdentity
{
    public int identity{ get; set; }
}
我不知道为什么要在IGenericRepository接口上将“id”作为属性公开-当然,这应该是传递给find方法的参数,如您的实现所示。您还需要从以下位置更改对“GetById”方法的限制:

where T : class
差不多

where T : IIdentity

使用我上面建议的接口。

这里有几个问题-第一个问题是您要匹配的泛型类型是类,但类没有名为“id”的属性。您需要让Category类实现一个公开“id”属性的接口:

public interface IIdentity
{
    int identity { get; set; }
}

public class Category : IIdentity
{
    public int identity{ get; set; }
}
public class IHasId
{
    public int id { get; set; }
}

public interface IGenericRepository<T>
{
    int id { get; }

    T GetById(int id);
}

public class GenericRepository<T> : IGenericRepository<T> where T : IHasId
{
    public int id
    {
        get { throw new NotImplementedException(); }
    }

    public T GetById(int id)
    {
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
}
我不知道为什么要在IGenericRepository接口上将“id”作为属性公开-当然,这应该是传递给find方法的参数,如您的实现所示。您还需要从以下位置更改对“GetById”方法的限制:

where T : class
差不多

where T : IIdentity

使用我上面建议的接口。

但是当我使用gr.GetById15时,它给了我一个错误,当我将where子句更改为where T:IHasID时,它表示无法将类别转换为IhasIdProperty、@DKNAACKfactly、@dknaack,然后dataContext.GetTable会给我一个错误这是一个带有更改存储库的新闻答案。但是当我使用gr.GetById15时,它会给我一个错误,当我将where子句更改为where T:IHasID时,它说不可能将Category转换为IhasIdProperty,@DKNAACKfactly,@dknaack,然后dataContext.GetTable会给我一个错误这是一个有更改存储库的新闻答案。谢谢你的回答@Dave。但问题是,当我将where T:class更改为IIdentity时,dataContext.GetTable将无法工作,因为T在该上下文中的工作方式类似于表名变量。或者我不能让它工作,我不知道…谢谢你的回答@Dave。但问题是,当我将where T:class更改为IIdentity时,dataContext.GetTable将无法工作,因为T在该上下文中的工作方式类似于表名变量。或者我不能让它工作,我不知道…该死!!我最终理解了这个问题,除了这些代码,我还必须为我使用的每个实体创建一个分部类。比如说,我有一个Category表,我必须用IBase继承创建一个公共部分Category!!谢谢你,伙计!!该死我最终理解了这个问题,除了这些代码,我还必须为我使用的每个实体创建一个分部类。比如说,我有一个Category表,我必须用IBase继承创建一个公共部分Category!!谢谢你,伙计!!
public class IHasId
{
    public int id { get; set; }
}

public interface IGenericRepository<T>
{
    int id { get; }

    T GetById(int id);
}

public class GenericRepository<T> : IGenericRepository<T> where T : IHasId
{
    public int id
    {
        get { throw new NotImplementedException(); }
    }

    public T GetById(int id)
    {
        return from tbl in dataContext.GetTable<T> where tbl.id == tid select tbl;
    }
}