2017-12-27 380 views
0

創建Web Service當我使用Entity Framework。 我有一個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表該類得到再生和我的解密邏輯在一起後更新從數據庫模型被刪除。

我如何能堅持,所以,我每次生成模型時,這種類只被新的內容更新。這有可能嗎?

+0

它作爲[部分類]生成(https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods)所以在那裏移動你的邏輯。 –

+0

我可以在我的項目 – Yev

+0

中看到只有一個部分類MyDBEntities。對,你需要添加另一個來保存你不想生成的代碼。 https://stackoverflow.com/questions/42160958/how-to-extend-an-entity-framework-6-1-3-generated-class –

回答

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; 

} 
相關問題