本文介绍了Asp.net MVC 2实体框架通用存储库方法。如何更新特定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个相同设计的表。每个人都有一个 IsActive 列。



例如:



类别

  CatID 
CatName
IsActive

产品

  PrdID 
PrdName
IsActive

是否一种创建通用方法来更新 IsActive 列的方法。

  public void Deactivate< T>(T TEntity)
{
//将代码更新
// IsActive
}

我阅读了关于通用存储库,但没有解释如何更新一个特定的列。



感谢大家。

解决方案

诀窍是在 BaseRepository 类上为您的泛型类型设置类型限制。尝试类似的东西:



警告:空中代码; - )



基本型号:

  public interface IDbTable 
{
bool IsActive {get;组; }
}

public class DbTable
{
public bool IsActive {get;组;
}

您的模型

  public class Category:DbTable 
{
public int CatId {get;组; }
public string CatName {get;组;
}

public class Product:DbTable
{
public int PrdId {get;组; }
public string PrdName {get;组;
}

您的存储库

  public interface IBaseRepository< T>其中T:class,IDbTable 
{
void Deactivate< T>(T entity);
}

public class BaseRepository< T> :IBaseRepository
{
public void Deactivate< T>(T entity)
{
entity.IsActive = false;
}
}

您可以进一步扩展您的IDbTable以包含甚至更通用和有用的列。公共接口IDbTable
{
int Id {get;组; }
bool IsActive {get;组; }
DateTime UpdatedOn {get;组; }
DateTime CreatedOn {get;组;
}

回购

  public interface IBaseRepository< T>其中T:class,IDbTable 
{
T GetById(int id);
void Add(T entity);
void Update(T entity);
void Deactivate(T entity);
}

public class BaseRepository< T> :IBaseReposiotry< T>
{
public T GetById(int id)
{
//通过id获取实体的代码
}

public void Add (T entity)
{
entity.CreatedOn = DateTime.UtcNow;
entity.UpdatedOn = DateTime.UtcNow;
}

public void Update(T entity)
{
entity.UpdatedOn = DateTime.UtcNow;
}

public void Deactivate(T entity)
{
entity.IsActive = false;
}
}

这两篇文章也应该帮助你: br>




HTHs,

Charles


I have 10 tables with the same design. Each of them have an IsActive column.

For example:

Category

 CatID
 CatName
 IsActive

Product

PrdID
PrdName
IsActive

Is there a way to create a generic method to update the IsActive column.

 public void Deactivate<T>(T TEntity)
 {
     // Put the code to update
     // IsActive
 }

I read about generic repository, but nothing explains how to update a specific column.

Thanks everyone.

解决方案

The trick is to put a where type restriction for your generic type on your BaseRepository class. Try something similar to this:

WARNING: air code ;-)

Base model:

public interface IDbTable
{
    bool IsActive { get; set; }
}

public class DbTable
{
    public bool IsActive { get; set; }
}

Your model

public class Category : DbTable
{
    public int CatId { get; set; }
    public string CatName { get; set; }
}

public class Product : DbTable
{
    public int PrdId { get; set; }
    public string PrdName { get; set; }
}

Your repository

public interface IBaseRepository<T> where T : class, IDbTable
{
    void Deactivate<T>(T entity);
}

public class BaseRepository<T> : IBaseRepository
{
    public void Deactivate<T>(T entity)
    {
        entity.IsActive = false;
    }
}

You could go even further and extend your IDbTable to include even more generic and helpful columns. E.g.

public interface IDbTable
{
    int Id { get; set; }
    bool IsActive { get; set; }
    DateTime UpdatedOn { get; set; }
    DateTime CreatedOn { get; set; }
}

Repo

public interface IBaseRepository<T> where T : class, IDbTable
{
    T GetById(int id);
    void Add(T entity);
    void Update(T entity);
    void Deactivate(T entity);
}

public class BaseRepository<T> : IBaseReposiotry<T>
{
    public T GetById(int id)
    {
        //code to get the entity by id
    }

    public void Add(T entity)
    {
        entity.CreatedOn = DateTime.UtcNow;
        entity.UpdatedOn = DateTime.UtcNow;
    }

    public void Update(T entity)
    {
        entity.UpdatedOn = DateTime.UtcNow;
    }

    public void Deactivate(T entity)
    {
        entity.IsActive = false;
    }
}

These two articles should help you out as well:
new Repository().DoMagic()
Implementing a Simple Generic Repository with LinqToSql

HTHs,
Charles

这篇关于Asp.net MVC 2实体框架通用存储库方法。如何更新特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:40