Read entities of entity framework from frontend - asp.net

Hello how can i access the entities generated by entity framework from front end?
from my business logic i can access it but i can't access it from frontend.
Can i move the entities from entity framework project to business logic layer or replicate them in it?
I think there should be an easy way to do it, because i can't belive microsoft has developed a pattern in whitch data layer should be referenced directly by ui, it is a thing people do when they are 8 years old

I will walk you through this from the Entity Framework 5 / VS2012 perspective. However other versions should work similarly.
Assuming from the above you have three projects (data/business/ui) and a entity framework context (.edmx) item in your data project.
Create a new project (class library) for your solution. This project will house your entity (POCO) objects so name it appropreatly.
Create a project reference from your data project to your new entities project
Create a new item in your new project (EF 5.x DbContextGenerator)
Delete the Context.tt file in the newly created item.
Open the other .tt file and modify the line that defines inputFile to map to your .edmx file
const string inputFile = #"../<dataprojectname>/<mydata>.edmx";
In your data project expand out "mydata".edmx and locate the entities template named "mydata".tt (you will know it is the correct one because it expands to show a list of your entities) and delete it.
Modify the "mydata".Context.tt file: scroll down until you see the using statments. Append the list of usings with the namespace of your entities created in step 3.
Now you can create references from your business and UI projects directly to your entities project.
Hope this helps.

Related

How to apply new changes to current database in code fist EF core?

I have a project that coded with DotNet Core code first.
Now, there is a lot of real data on the database and i have to add some features with many tables and new relations to this project.
How can I apply these tables and their relations to the Database without adding manually?
Is there any way to use Add-Migratiosn and Update-Database to apply new changes via EF core instead of manually applying?
If you have not many changes to your DBContext you can remove it and use Scaffolding to create new DbContext from the existing database schema. You can find more information in the official Microsoft documentation here.
It is better to use only one entity framework database approach either Database-First or Code-First.

How to add reference from WebAPI to DataModel in ASP.NET WebAPI Solution

I have created one solution and inside that i have created 2 projects, one for the web api and the other for the data model. How can i use the data model in the web api project?
Right click the project you want to use the data type in
Tick all the projects you want the selected project to have access too
Add a using statement at the top of every class file that you want to use the data model in
Why 2 solutions. You can actually have one solution file with 2 projects in it.
Once you build the solution you can see a dll in your data model project. You have to add reference of that dll in your project web api by right clicking on the reference and add reference where your data model project will be there. Click and add. You are done. Access data model in webapi by using right namespace.

Is it possible to use Entity Framework Code First with Web Forms (Asp.NET)?

It is possible to use ASP.net EF Code first approach with Web Forms 4.5.2 ? I am currently using web forms to database first and would love to switch to code first approach.
Whether it is a console application or a complex engine project, the Data Access Layer can have any approach. So the direct answer is: yes you can.
You can generate code first classes and context from an existing database
All you need is to install Entity Framework 6.x (if not already done), then follow these instructions:
3.Reverse Engineer Model
Project > Add New Item…
Select Data from the left menu and then ADO.NET Entity Data Model.
Enter BloggingContext as the name and click OK.
This launches the Entity Data Model Wizard. Select Code First from Database and click Next.
Follow the wizard and it will generate the models and context with the connection

Entity Framework Core - Scaffold existing database to one or group of class files

As stated in this tutorial, I can scaffold code-first EF Core from an existing database using Scaffold-DbContext in the Package Manager Console.
It's all good but my problem is that it scaffolds separate class files for each table and another for the context class. I find this a bit messy if I have too many tables.
I can rearrange it all by hand but Is there a way to scaffold everything to one class file? Or maybe having the context class on a separate file and then group the tables to another file per schema.
This behavior is not an option of EF Core scaffolding as of 1.0.0.

i18n in asp.net using resources and entity framework

I've been trying to store my strings in a database using resource files to fetch them. I've been following the guide from http://afana.me/post/aspnet-mvc-internationalization-store-strings-in-database-or-xml.aspx
The example works fine enough when i use his sql code based on ADO.net to fetch the strings from the database.
But i want to use Entity Framework to fetch the strings from the database, because my translation table is quite different. Could anyone show me an example of how to use Entity Framework in the resource project(assembly?), because i can't seem to get it to work. I cant import any of my models or contexts from my main project with "using", and it tells me i have duplicate models when i copy them to the resources project/assembly

Resources