Questions tagged [entity-framework]

Entity Framework is a LINQ-based object-database mapper for .NET. It supports change tracking, updates, and schema migrations for many databases. Please add a version-specific tag as well.

Entity Framework is .NET's Object-Relational Mapping (ORM) tool that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Either natively, or through third-party libraries, it supports most major RDBM products including SQL Server, MySQL, Oracle, PostgreSQL and SQLite. It also supports Microsoft's "LINQ" syntax and lambda-expressions.

Entity Framework was first developed for .Net framework (production-ready versions 4.x - 6.x). In 2016, Entity Framework for .Net core (EF-core) was introduced, having a completely new codebase which shares many concepts with the classic framework but differs considerably in mapping syntax, query translation and specific features. After version 1 and 2, EF-core 3 came with many breaking changes, marking the beginning of a more stable evolution path. EF core 5 (versions keep track with .Net core versions) has far less breaking changes.

As of version 6.3.0, EF 6 is cross-platform. It targets .Net standard 2.1. NuGet packages are available for .Net core 3 and .NET Framework 4.x.

Because of all these different versions it's very important to use the right tags when asking questions.

Entity Framework is well-documented.

91477 questions
21
votes
6 answers

The type appears in two structurally incompatible initializations within a single LINQ to Entities query

I'm trying to build something like conditional queries to get only needed data from the underlying database. Currently I have the following query (which works fine) var eventData = dbContext.Event.Select(t => new { Address = true ? new…
KingKerosin
  • 3,639
  • 4
  • 38
  • 77
21
votes
2 answers

Include property but exclude one of that property's properties

Let's say I have a method like this in one of my controllers: [Route("api/Products")] public IQueryable GetProducts() { return db.Products .Include(p => p.Category); } Using this I can get a product from the database and…
tobloef
  • 1,821
  • 3
  • 21
  • 38
21
votes
1 answer

WithOptional with Entity Framework Core

I'm trying to migrate my old app to the new EF Core but I cannot find some relationships like: HasRequired(o => o.Document).WithOptional(o => o.CancelNote); Is there some extension methods? I cannot find on the docs. The HasRequired I think that…
gog
  • 11,788
  • 23
  • 67
  • 129
21
votes
5 answers

AutoMapper throwing StackOverflowException when calling ProjectTo() on IQueryable

I have created classes using EF Code First that have collections of each other. Entities: public class Field { public int Id { get; set; } public string Name { get; set; } public virtual List Teachers { get; set; } public…
Peter
  • 449
  • 1
  • 3
  • 13
21
votes
1 answer

There is no argument given that corresponds to the required formal parameter 'context of GenericRepository.GenericRepository(dbContext)

I am getting this error message which when trying to inherit from my GenericRepository. The error says I need to also provide a context but I am not sure how? //IncidentRepository public class IncidentRepository :…
21
votes
4 answers

Setting EF Connection String in Azure Web App

We have an ASP .NET (MVC) app and are using Entity Framework 6 to connect to our databases. The DbContext is constructed in a standard way and it loads the connection string on our behalf. The generated code looks like this: public partial class…
21
votes
2 answers

Entity Framework proper way to replace collection in one to many

Suppose a customer has many phone numbers and a phone number has only one customer. public class PhoneNumber : IValueObject { public string Number {get; set;} public string Type {get; set;} } public class Customer : IEntity { public…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
21
votes
2 answers

Entity framework code first migrations, sql user permissions?

What is the minimal permission needed on a sql server user/login for it to be able to run entity framework code first database migrations? I naively would have thought that a user with the roles db_datareader, db_datawriter, Grant Alter on the…
21
votes
1 answer

Automapper many to many mapping

Patrick, thanks for advice about correct question! EDIT 1: I have three table for many to many relationship. Like this: GoodEntity: public partial class GoodEntity { public GoodEntity() { this.GoodsAndProviders = new…
Hellaren
  • 422
  • 1
  • 5
  • 15
21
votes
3 answers

Should I separate my application context from ApplicationDbContext used for identity?

In Visual-Studio 2013, when creating an ASP.NET project, it generates a file IdentityModels.cs that contains a class ApplicationDbContext, that inherits from IdentityDbContext, which eventually inherits from DbContext. Should I keep…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
21
votes
3 answers

Entity Framework (Database-First) multiple relations to same table naming conventions control

Let's suppose that we have this situation: Tables in database: Country (id, country_name), Person (id, login), CountryManager (id_country, id_person), CountryStakeholder (id_country, id_person) If we had to create the model from the database, using…
21
votes
2 answers

Deleting file, but is access denied

I have an mvc4 application with entity framework. I want to delete a file, but every time it says: An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code Additional information: Access to…
21
votes
15 answers

MySQL Connector with EF6 in Visual Studio 2013

I am attempting to connect to MySQL through a C# .NET web MVC application. My issue is that, when I attempt to add an ADO.NET Entity Data Model, generated from Database, based on my MySQL connection, I get the following error message: Your project…
Olive
  • 3,516
  • 7
  • 24
  • 32
21
votes
2 answers

More than one migrations configuration type was found in the assembly 'SMSApp'. Specify the name of the one to use

I am developing a mvc 5 application using code first approach. i am facing an issue . at first time when i try below comands , it worked and generate the table in that database. but when i changed some more classes and then tried with first 2…
maifs
  • 1,061
  • 6
  • 15
  • 31
21
votes
3 answers

Entity Framework 6.1.1 disable model compatibility checking

I am running into the following error after updating EF to version 6.1.1: An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll Additional information: The model backing the TvstContext context has…
Tukurai
  • 213
  • 1
  • 2
  • 4
1 2 3
99
100