EF class context - c#

I have Microsoft VS 2010.In C# for retrieving data from database i use Ado.Net entity data model.This is Entity Framework?
I see in many articles that context created by Entity Framework is derived from dbContext but in my project context is derived form ObjectContext .
So, what is EF?

EF 4.0 (which is part of the .net Framework) uses System.Data.Entities.ObjectContext from EF 4.3 (which was Opensourced project and is not part of the Framework) onwards uses System.Data.Entities.DbContext.
Both are EntityFramework. One is newer. Both uses ADO.NET under the covers.

Yes, it is Entity Framework. However, context should be derived from ObjectContext. The ObjectSet
Represents a typed entity set that is used to perform create, read,
update, and delete operations.
and it was introduced in .NET 4.0
You may also take a look on this for additional information on object sets.

Related

.Net core 3.x Keyless Entity Types avoid table creation

I need to execute a complex sql query in entity framework core 3.1.1, on researching i found out that keyless entity types is the way to go in code first approach. I see lot of documents for dbquery but this is marked as obsolete in .net core 3.x
keyless entity types
As per Microsoft documentation it says dbquery is obsolete so use dbset approach instead, but with dbset it is trying to create a new table in database. how to disable table generation in keyless entity types while applying migrations?
Sample code
public class ApplicationContext : DbContext
{
public DbSet<CustomQuery> CustomQuery { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<CustomQuery>();
modelBuilder.Entity<CustomQuery>().HasNoKey();
}
}
with .net core 2.2
var entity = _context.Query<CustomQuery>().FromSqlRaw(Regex.Unescape(selectQuery)).AsNoTracking().FirstOrDefault();
with .net core 3.1
var newEntity = _context.CustomQuery.FromSqlRaw(Regex.Unescape(selectQuery)).AsNoTracking().FirstOrDefault();
if i try to apply migrations then a new table in the name of custom query is being created, but I don't need this to happen. because this is just an model used to hold the values from the join query and i will not insert, update or delete the values in this table. how to achieve this?
or is there any better approach for this situation.
This is a known defect in EF Core 3, reported here 3.0 Upgrade - Entity with HasNoKey() (formerly a query type) tries to create tables when adding migration #18116.
Closed as "duplicate" of To vs From methods: Proposal to rationalize ToTable, ToQuery, ToView, FromSql, and other related methods #17270 and Ability to exclude/skip/ignore parts of the model from migrations so that a table is not created (for overlapping bounded contexts) #2725, both scheduled for 5.0 release, which means it would eventually be addressed in that release.
The current workaround is mentioned in the comments by one of the EF Core team members:
For now, you can just use something like .ToView("You forgot to use FromSql with ModQueueEntry")
or more generally, using .ToView(null), e.g.
modelBuilder.Entity<CustomQuery>().HasNoKey().ToView(null);

How can I map my entity to a table in .Net Core project without using Entity Framework and only using ADO.NET

I have a project for Entities and I am using ADO.NET code for normal CRUD operations on this entity. I would like to map my entity name to table names, Could you please provide me a way to do it. With .Net Standard I could use System.Data.Linq.Mapping. But my project is in .Net Core 2.2 and I cannot add these as a reference, tried changing target framework, but my other projects are not compatible. Kindly advise me how do I do it.
I can see a couple of options.
1) Create a Mapper class that accepts T1 & T2.. And do the mapping manually for each property.
2) Use a library such as Automapper that does all this for you automatically (https://github.com/AutoMapper/AutoMapper)

In 2017, what is the best way to migrate an entity framework application away from using EDMX while preserving the relationships

I've seen a number of old question's asking the same (Tool to convert Entity Framework EDMX to Code First) but most back in 2009-2014 (EF "Model First": Create entity in edmx and db-table from class? / How to generate Entity Framework 6.x POCO classes with mappings from an EDMX file?) so would like to ask if the situation is any better in 2017!
We have an EDMX, which we've already set to use the DbContext generator- this gives cleaner POCO's and a leaner context derived from DbContext and not the now ancient ObjectContext but would love to totally ditch the (completely unmergeable) edmx, however keeping the many many relationships we have between the 60-odd entities we have. Simply removing the tt templates currently in place, loses all that entity relationship data.
Is there now a tool or methodology which could help achieve this? I can't believe that Microsoft themselves haven't released a tool (the powertools aside as I believe they don't generate the relationships)

Entity Framework web or Entity Object

I have got a assignment question from my college, there is a line as follows
This must be implemented using C# and utilize the Entity Framework classes.
this means Entity Objects or Entity Framework??
and can I implement Entity Framework as a C# desktop application
please help me to understand this. thank you
Yes you can use Entity framework for desktop application. Create one project as desktop application and add another as the business logic project, this should be one that uses entity framework objects to communicate with your database. Lastly, another project that will implement all your desired methods in relation to the entity framework project. This is the project that the windows application communicate with directly. Check out here for some tutorial
You can combine it as one project but I prefer separation of concern so you can compile the business logic as a dll. Entity framework is just one of the object relation mapping model and it can be used across various projects types. Others include NHibernate, LINQ to SQL etc
it is a library that can be used either on web or desktop .NET project: You can get it here

What is the Major Difference between Entity Framework of ASP.Net 4.0 and Entity Classes which is written by developer?

Before entity Framework, Developer was writing the code Entity Classes which is contains the getter & Setters Method for Data Table Fields (Columns).
what is the purpose of introduce Entity Framework, and what's different between Entity Framework and Older traditional way to write down Entity Classes?
If you do not use EF (or any other ORM tool), you will have to write both entity classes and related database operations by hand.
ORM tools creates both entity classes and an abstraction of related DB operations automatically.
In case of EF, it creates entity classes and an ObjectContext (or a DBContext) which allows you to manipulate DB entities without writing SQL code.
Entity Framework provides ORM capabilites to Entity Classes. You don't need to craft CRUD or Database operations on database layer, EntityFramework handles it.

Categories

Resources