Reorganizing codes after generating it using ADO.NET DbContext Generator - asp.net-mvc-3

Suppose I am creating a application using the sample Northwind database using asp.net mvc 3 and entity framework database first approach. For that I am opening a new asp.net mvc 3 project and then adding a ado.net entity data model. I am generating it from the existing database as it is already created. After that I am using the ado.net DbContext generator to generate codes for me. These includes all the models and the DbContext file(in this case NorthwindContext).
Now the problem that I am facing is in reorganizing the code. All the models should be placed in the Model folder of the project. Even if i generate the codes in the Model folder the NorthwindContext class is also generated in it. That is under the NorthwindContext.tt file a NorthwindContext.cs is generated. This file should reamin in the DAL folder as I will use it for accessing the data. Now if i just drag an drop it into the DAL folder the code generation stops automatically(no code remains in the NorthwindContext.cs file). Any idea how i can resolve this problem?????

There are a few hoops you need to jump through to get this setup.
You should have two T4 templates generated, one for the model classes and one for the context. Move the T4 template for the model to your model assembly then open it in Visual Studio and update the file path back to the edmx in your data access assembly. Edit the context T4 in the data access assembly to emit a using statement for the model namespace. Finally add a reference to your model assembly in the data access assembly.
Whenever you want to regen the classes right click in VS and select Run Custom Tool for both the Context and Model T4.

Related

How to use one database in two .Net core MVC and Web API projects?

I've create two MVC and Web API .Net Core projects in one solution, in first project i've added model Phone and DataContext for it, made migration and update-database. In second project i've added equals connectionstring,model and DataContext, but when i try make the update-database i get the next error:
There is already an object named 'Phones' in the database.
Tell me please the right way how to configure database, datacontext and models in two projects to use one database ?
If you use two separate DbContexts, each with their own migrations, they'll consider the database to be theirs and reapply the changes that the other project already did, resulting in conflicts such as the one you observed.
Simply move the Entity Framework code into a shared class library and reference that library from both implementing projects, so all state about the database is shared.

How to develop project using T4 template that can create database,database tables etc like entity framework works

I want to use T4 template for generation of code,that can take input from windows form and create my projects depend on input of form.I want to create database,database tables like entity framework create the database module for us.
Thanks in advance.
Use T4 templates in runtime mode:
https://msdn.microsoft.com/en-us/library/ee844259.aspx
Collect your data from form and pass them to T4 template (in constructor for example).
Constructor create in partial class for template.

How to Share Models Between WP7 and Azure Projects

I have a WP7 project, which will invoke a REST web service in Azure (MVC4 WebApi).
My WP7 project has models that it serializes to JSON and then sends to the web service.
The web service deserializes the data sent from WP7 and instantiates the models again before saving them to Azure Table Storage.
How can I share the Model classes between the projects? Right now I'm just copying the cs files over, and I have to update both sets if I make a change to the models. I was hoping a simple class library project would be able to be referenced from both projects, but WP7 couldn't handle that.
What should I do?
Thanks!
There are many solutions for this issue:
You could use a T4 template to read the entity and generate a class your WP7 project that only contains the properties of the object without reference to the Table Storage specifics (like TableStorageEntity): http://weblogs.asp.net/cibrax/archive/2009/03/11/code-generation-with-t4-an-entities-to-dto-example.aspx
You could split your entity over 2 files, one with the TableStorage specifics like TableStorageEntity and one file containing only the properties of the entity (use partial classes for this). Then you can add the file containing only the properties in your WP7 project as a link.
Create a DTO (or whatever you call it) class manually and use something like AutoMapper to map between the DTO and the TableStorage entity. Store the DTO in a portable library so it can be used by every kind of project. In my opinion this is the best solution since you don't want to completely expose your entities to "the outside world". An example would be a list of users. You wouldn't want to return all fields including password, hash... and other sensitive info. It would be better to have a separate class that only contains the info you want to expose externally.

Does MVC Code first eliminate the need to use DB projects to source control the DB and be in sync?

We are planning a rewrite and I want to get the latest tools to make sure we are doing things the best way. I like how code first keeps the DB structure in code.. which is already source controlled.. So I am hoping that would remove the need for a DB project.. Yes? No ?
DB projects also allow you to sync data between enviorments but that is something else that has nothing to do with code first
Using Entity Framework Code First you can write things out in C# code and have it create/persist the data. Since you can source control C# code, you can source control the database, but only the schema. You won't be able to 'source control' the data.
Personally, I don't use it because it forces you to do some wonky stuff such as declaring properties as virtual and using decorators all over the place. Not only that but you really on the framework to do a lot of things behind the scenes and when things go wrong that's never good.
The route I choose and that works very well for me is to declare a Project.Domain class library project with the Entity Framework generated model from an existing database.
Then I would reference this project from my Project.WebUI (my MVC3 project) and have access only to public repository classes.
So my project looks like:

How to use EF4 as a DAL

I am currently using EF 4 with my ASP.NET Website to access a MySql database. This works fine. However I now want another Web Site project to access the same entities. How do I set this up?
I can't just reference the original website as it's a Web Site, not a Web Application. So presumably, I need to put the Entity Data Model in its own project and compile to a DLL. But...
Which project type?
Do I just cut and paste the DataModel.edmx and DataModel.Designer.cs, compile and add a reference in both websites? What about namespaces?
Where do I put the connection string? At the moment it's in my project's Web.config.
I really have no idea where to begin - I've just followed tutorials to get EF working up to now! I'd really appreciate step-by-step instructions if anyone has time. Thanks.
The model should be placed in a new class library project. My preference would be to recreate the model at this point based on the existing model. For the namespace I like to use {CompanyName}.DataAccess. Remove the old model from your web site project, add a reference to the new class library project and build the web site project. The website project will break in many places, but it should be a simple matter of changing the namespace to the new data access assembly. I prefer this method as to cut/paste because now you have nice clean namespaces. Be careful of any places you may have strings with entity names in them, like if you were using Include (if you are using EF 4 and lazy loading, this should not be a problem). Leave the connection string in web.config for both of the web site projects. When you create the model in the class library, it will add a connection string in app.config. That is OK, it is just there so the model knows how to connect to the database when you refresh it.

Resources