In Existing project implementation done from edmx model first approach EF I have to use Code First approach . In existing project developed creating repository BO ,base Bo ,Enum classes . Have to reflect the same with just change in approach.
1 Answers
You can use the EntityFramework Reverse POCO Generator Visual Studio extension. As its documentation says:
Reverse engineers an existing database and generates EntityFramework Code First POCO classes, Configuration mappings and DbContext.
Also, you can use the Entity Framework Power Tools Visual Studio extension, which includes more functionalities, like generating diagrams of your data models.
To use EF PowerTools with Visual Studio 2015 do as explained in this post.
In addition to generate your POCO entities and database context you will have to enable the use of migrations with the Package Manager Console and to generate at least an initial migration for your database. You will get a migration Configuration class, and will need to configure a Database Initializer. This page explains the basics of these steps.
-
helpful post ,but Actually I created Database table to classes by doing it programmatically now I have to create a repository classes which was actually developed in current project using edmx it support a entity Object, object Context object set so the depending properties uses CreateObjectSet to be call with it so I have to create a similar architecture which will just changes from model edmx to code first ObjectStateManager repositoryBO.Context.Refresh(RefreshMode.StoreWins, collection); – bhaskar shelar Oct 28 '16 at 08:05
-
ObjectContext and ObjectSet
are mostly obsolete, you would benefit a lot of refactoring them to DbContext and DbSet – Diana Oct 28 '16 at 16:55. Which version of EF are you using? With EF6 you must use DbContext and DbSet. For any BO entity you would define a public property of type DbSet in your DbContext. Use them from the repository like this: instead of CreateObjectSet use myDbContext.MyEntityDbSet.Add() to insert a new entity, .Find() to fetch existing entities, and other available Linq methods to do the different operations you need. -
The MSDN page for ObjectStateManager Refresh contains examples of code using DbContext and DbSets: in that code the variable `context` is a DbContext, and `context.SalesOrderHeaders` is a DbSet, and it is used like this: `ObjectQuery
orders = context.SalesOrderHeaders.Where( "it.CreditCardApprovalCode IS NULL").Top("100");`. And also like this: `context.Refresh(RefreshMode.ClientWins, orders);`. Link to the page: https://msdn.microsoft.com/en-us/library/bb896255%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – Diana Oct 28 '16 at 17:14 -
And here you have a simple example of how to write an EF code-first repository: http://stackoverflow.com/a/16135990/1182515 – Diana Oct 28 '16 at 17:22
-
I gone through all You mention . I am using latest EF 6.1.3 For code First approach. But In existing project there were a relationship with objectset at a lot of places & applicable for any table(T) T=t=dbTablename .(dynamically On a dbtable which we are working . In the new approach if I will try to go with use myDbContext.MyEntityDbSet.Add() instead of CreateObjectSet so I have to use for every particular table & the architecture level also not supportive for doing that for every operation . This is situation where I am stuck now Looking forward for Your Help, Thank You so much. – bhaskar shelar Nov 03 '16 at 05:03
-
I'm afraid I can not provide much more help, I don't have much knowledge of the old `ObjectContext` model. I added a comment in your other post. Sorry! I hope you will find a suitable solution for your project, best of luck to you! – Diana Nov 04 '16 at 01:37
-
Why Are You afraid ? The actual implementation was Like that & I have to use Code First approach With It & mainly code First approach Supports DbContext & In old Project They used A object Context So regarding the same I would like to find the answer There were A lot of dependencies so directly changing The object Context to Dbcontext Will Work or Not ? – bhaskar shelar Nov 04 '16 at 04:44
-
I don't know if directly changing from ObjectContext to DbContext will work, it seems that it will not work but I can't tell you for sure. It looks like you will have to do a lot of code changes. – Diana Nov 04 '16 at 10:25