Asp.net MVC 2的实体框架的通用仓库方法。如何更新特定Collumn [英] Asp.net MVC 2 Entity Framework Generic Repository Method. how to Update a specific Collumn

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

问题描述

我有10桌用相同的设计。他们每个人都有一个isActive Collumn。

EX:

类别

CATID

CatName

IsActive

产品

PrdID

PrdName

IsActive

有没有一种方法创建一个通用的方法来更新IsActive列。

 公共无效停用< T>(T TEntity)已
    {
        //把code更新
        // IsActive
    }

我读到关于通用信息库,但没有说明如何更新特定Collumn。

谢谢大家。


解决方案

诀窍是把其中, BaseRepository 类。尝试类似的措施:

警告:空气code; - )

基本型号:

 公共接口IDbTable
{
    布尔IsActive {搞定;组; }
}公共类DBTABLE
{
    公共布尔IsActive {搞定;组; }
}

您模型

 公共类类别:DBTABLE
{
    公众诠释CATID {搞定;组; }
    公共字符串CatName {搞定;组; }
}公共类产品:DBTABLE
{
    公众诠释PrdId {搞定;组; }
    公共字符串PrdName {搞定;组; }
}

您库

 公共接口IBaseRepository< T>其中T:类,IDbTable
{
    作废停用< T>(T实体);
}公共类BaseRepository< T> :IBaseRepository
{
    公共无效停用< T>(T实体)
    {
        entity.IsActive = FALSE;
    }
}

您可以更进一步,延长IDbTable包括更加通用的和有益的列。例如。

 公共接口IDbTable
{
    INT标识{搞定;组; }
    布尔IsActive {搞定;组; }
    日期时间UpdatedOn {搞定;组; }
    日期时间CreatedOn {搞定;组; }
}

回购

 公共接口IBaseRepository< T>其中T:类,IDbTable
{
    ŧGetById(INT ID);
    无效添加(T实体);
    无效更新(T实体);
    无效停用(T实体);
}公共类BaseRepository< T> :IBaseReposiotry< T>
{
    公共ŧGetById(INT ID)
    {
        // code度日ID的实体
    }    公共无效添加(T实体)
    {
        entity.CreatedOn = DateTime.UtcNow;
        entity.UpdatedOn = DateTime.UtcNow;
    }    公共无效更新(T实体)
    {
        entity.UpdatedOn = DateTime.UtcNow;
    }    公共无效停用(T实体)
    {
        entity.IsActive = FALSE;
    }
}

这两篇文章应该帮助你还有:结果
新的存储库().DoMagic()结果
<一href=\"http://aleris.word$p$pss.com/2009/04/17/implementing-a-simple-generic-repository-with-linqtosql/\"相对=nofollow>与LinqToSql

HTHS,结果
查尔斯

I have 10 tables with the same design. Each of them has an IsActive Collumn.
EX:
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 explain how to update a specific Collumn.
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的实体框架的通用仓库方法。如何更新特定Collumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆