从数据库更新模型后如何保持实体代码不变?

时间:2017-12-27 19:04:19

标签: entity-framework dbcontext

我在创建Entity Framework时使用Web Service。 我有一个Entity班级DBContext

在这个课程中,每次创建context时,我都会编写一个处理密码解密的逻辑:

namespace ePdfExtractor.Entity

public partial class MyDBEntities : DbContext
{
    public MyDBEntities()
        : base()
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<MyTable1> MyTable1 { get; set; }
    public virtual DbSet<UserSvc> UserSvcs { get; set; }
}

这是我的实体另一个部分类:

namespace ePdfExtractor.Entity

public partial class MyDBEntities 
{
    public MyDBEntities(): 
           base(GetDecrptString())
    {
    }
    public static string DecrypConnectionString(string connectionString)
    {
        return EncDec.EVODecrypt(connectionString);
    }

    public static string EncodeConnectionString(string connectionString)
    {
        return EncDec.EVOEncrypt(connectionString);
    }

    private static string GetDecrptString()
    {
        string connString = ConfigurationManager.ConnectionStrings["MyEntitiesConnection"].ConnectionString;
        return EncDec.EVODecrypt(connString);
    }
}

每当我需要查询表时,我都会创建MyDBEntities的新实例:

public partial class UserSvc
{
    public static UserSvc Authenticate(string userName, string password)
    {
        using (var ctx = new MyDBEntities())
        {
            UserSvc userObj = ctx.UserSvcs.FirstOrDefault(u => u.UserName == userName && u.Password == password && u.Active);

            return userObj;

        }
    }
}

在创建新的UserSvc表后从数据库更新模型时,此类会重新生成,并且我的解密逻辑和构造函数的定义将被删除。

我如何坚持下去,因此,每次重新生成模型时,此类只会更新新内容。这可能吗?

1 个答案:

答案 0 :(得分:0)

您需要为分部类设置不同的签名:

namespace ePdfExtractor.Entity

public partial class MyDBEntities 
{
    public MyDBEntities(string connectString): 
           base(GetDecrptString(connectString))
    {
    }

    public static string EVOConnectionString(string connectionString)
    {
        return ConfigurationManager.ConnectionStrings["MyEntitiesConnection"].ConnectionString;
    }

    public static string DecrypConnectionString(string connectionString)
    {
        return EncDec.EVODecrypt(connectionString);
    }

    public static string EncodeConnectionString(string connectionString)
    {
        return EncDec.EVOEncrypt(connectionString);
    }

    private static string GetDecrptString(string connString)
    {
        return EncDec.EVODecrypt(connString);
    }
}

然后使用新签名调用它:

using (var ctx = new MyDBEntities(MyDBEntities.EVOConnectionString))
{
    UserSvc userObj = ctx.UserSvcs.FirstOrDefault(u => u.UserName == userName && u.Password == password && u.Active);

    return userObj;

}

你也可以做一个黑客强迫部分:

namespace ePdfExtractor.Entity

public partial class MyDBEntities 
{
    public MyDBEntities(string dummy): 
           base(GetDecrptString())
    {
    }
    public static string DecrypConnectionString(string connectionString)
    {
        return EncDec.EVODecrypt(connectionString);
    }

    public static string EncodeConnectionString(string connectionString)
    {
        return EncDec.EVOEncrypt(connectionString);
    }

    private static string GetDecrptString()
    {
        string connString = ConfigurationManager.ConnectionStrings["MyEntitiesConnection"].ConnectionString;
        return EncDec.EVODecrypt(connString);
    }
}

然后使用新签名调用它:

using (var ctx = new MyDBEntities("Dummy"))
{
    UserSvc userObj = ctx.UserSvcs.FirstOrDefault(u => u.UserName == userName && u.Password == password && u.Active);

    return userObj;

}