c# - How to Unit Test with Dynamically-Generated ADO.NET Entity Data Model Code -
I follow the following which tells the fake method of the entity framework when doing unit testing on a web API 2 service.
Defines an interface
by using code dependency injection name space StoreAcreTransmissions.Models {public interface IStoreAcreTransmissionsContext: IDisposable {DbSet & LT; AcreReportTransaction & gt; AcreReportTransactions {Receive; } Inch checks (); Redundant markmemsodified (acre reporting item); }} is then implemented by a class that
name space StoreAcreTransmissions.Models {public class StoreAcreTransmissionsContext: DbContext, IStoreAcreTransmissionsContext {// you custom The code can not be overwritten in this file. // // If you want to drop and reproduce the Entity Framework to your database, then whenever you change your model schema, please use data migration. // See the documentation for more information: http:// http://msdn.microsoft.com/en-us/data/jj591621.aspx Public StoreAcreTransmissionsContext (): Base ("name = StoreAcreTransmissionsContext") {} Public System.Data. Entity.DbSet & LT; StoreAcreTransmissions.Models.AcreReportTransaction & gt; AcreReportTransactions {Receive; Set; } Public Zero Marksemodified (Acre Reporting Item) {Entry (Item) .set = Entitestate. Revised; }}} and passed the controller's creator
Public AcreReportTransactionsController (IStoreAcreTransmissionsContext Reference) {db = reference; } All this works very well, but in the normal operation of the service I am using an auto-generated ADO unit data model to communicate with the database. So to use the actual data model in my controller, I have to set it up in the default constructor:
private isotrr editcontact db; Public AcreReportTransactionsController () {db = New DataModelContext (); } But I also have to modify the dynamically generated reference class ( AcreReportTransactionModel.Context.cs ) that is generated by the model so much It also implements the IStoreAcreTransmissionsContext interface:
// ----------------------------- ------- ------------------------------------------ / & Lt; Auto Generated & gt; // This code was generated from a template. // // Manual change in this file may cause unexpected behavior in your application. // If the code is reborn, the manual changes in this file will be overwritten. // & lt; / Auto-generated & gt; // ------------------------------------------------ ------------------------------ Namespace StoreAuthorities Models {Use of System; Using System.Data.Entity; Using System.Data.Entity.Infrastructure; Public partial class ACRSIEntities: DbContext, IStoreAcreTransmissionsContext {public ACRSIEntities (): base ( "Name = ACRSIEntities") {} protected override void OnModelCreating (DbModelBuilder modelBuilder) {throw new UnintentionalCodeFirstException (); } Public Virtual DBSet & lt; Acre Rport Reporting & gt; AcreReportTransactions {Receive; Set; } Public Zero Marksemodified (Acre Reporting Item) {Entry (Item) .set = Entitestate. Revised; }}} The problem is, every time I re-sync my model with the database, this dynamically-generated reference class is rebuilt And the ISSTORMRCMCNCTX interface is removed.
How can I configure my code like unit test works and I can reproduce my model without adding code again?
You should not modify the generated code. Modify the template used to generate the code.
Open your EDMX-model tree in Solution Explorer, here you find a file named * .ontext.tt, open it and modify the appropriate section.
Comments
Post a Comment