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, when applicable.

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.

86622 questions
19
votes
4 answers

Display empty textbox using Html.TextBoxFor on a not-null property in an EF entity

I am using Entity Framework (v4) entities. I have an entity called Car with a Year property of type integer. The Year property does not allow NULL. I have the following in my Create view: <%= Html.TextBoxFor(model => model.Year) %> I am required to…
thd
  • 1,793
  • 6
  • 28
  • 44
19
votes
3 answers

LINQ Order By Descending with Null Values on Bottom

I have this expression: troubletickets = db.ServiceTickets.Include(t => t.Company).Include(t => t.UserProfile); troubletickets.OrderByDescending(t => t.UserProfile != null ? t.UserProfile.FirstName : "ZZZ"); I have to check if UserProfile is null…
mdk09
  • 277
  • 1
  • 4
  • 16
19
votes
5 answers

Unique keys in Entity Framework 4

An existing DB schema has unique, non-primary, keys, and some foreign keys that rely on them. Is it possible to define unique keys, which are not primary keys, in Entity Framework v4? How?
Asaf R
  • 6,580
  • 8
  • 41
  • 64
19
votes
2 answers

How to execute a full text search using entity framework 6

I have the query: var query = DataContext.Fotos.Where(x => x.Pesquisa.Contais("myTerm") The SQL generated is: SELECT ... FROM Fotos AS [Extent1] WHERE [Extent1].[Pesquisa] LIKE N'%mytem%' But I need to use: SELECT ... FROM Fotos AS…
Eduardo Silva
  • 311
  • 1
  • 2
  • 8
19
votes
4 answers

does not have a corresponding column in the data reader with the same name

I have Table named tbl_search with columns : id(int), title(nvarchar100), result(ntext) and i want using SQL query, like this : using (var db = new GoogleEntities()) { const string selectCmd = @"Select top 1 title…
pejman
  • 1,924
  • 5
  • 23
  • 52
19
votes
3 answers

Error 6046: Unable to generate function import return type of the store function

I have a scalar-valued function in my sql database. I receive this error when importing this function into Entity Framework model: Error 6046: Unable to generate function import return type of the store function 'GetContentByIdAndCul'. The store…
Sayed Abolfazl Fatemi
  • 3,018
  • 2
  • 29
  • 40
19
votes
6 answers

Entity Framework : Change connection string at runtime

Assuming there is an ASP.NET MVC application that uses Entity Framework 6 with code-first approach and StructureMap as IoC. Also It uses Unit Of Work pattern. Here are the codes : Domain Class     public class Product     {         public int Id…
Shahin
  • 11,703
  • 37
  • 120
  • 199
19
votes
1 answer

Entity Framework DB-First, implement inheritance

I'm trying to implement inheritance using entity framework 6.0 and database first approach. OK, let's say I have a Person and an Organization entity like below: // a simplified version of organization entity public class Organization { public…
Ashkan
  • 3,182
  • 4
  • 34
  • 45
19
votes
5 answers

Entity framework memory not released

I'm using a very simple asp.net mvc application with Entity Framework 6.0.2, .Net 4.5.1: public class HomeController : Controller { public ActionResult Index() { int count; using (var db = new LocalContext()) { count…
John Landheer
  • 3,941
  • 4
  • 27
  • 53
19
votes
2 answers

unable to retrieve metadata for unrecognized element providers

I get a message error whenever I try to add a controller with Entity framework template but I keep getting a error message unable to retrieve metadata for 'path' unrecognized element providers. …
19
votes
3 answers

EF 6 IsRequired() allowing empty strings

In past projects with versions of EF5 and EF4, the IsRequired() fluent API method would thrown a DbEntityValidationException if the property was null or an empty string. In my current project utilizng EF6, The DBEntityValidationException is not…
awolske
  • 191
  • 1
  • 1
  • 3
19
votes
3 answers

Cannot insert the value NULL into column in ASP.NET MVC Entity Framework

When trying to use this code: var model = new MasterEntities(); var customer = new Customers(); customer.Sessionid = 25641; model.Customers.Add(customer); model.SaveChanges(); I get: {"Cannot insert the value NULL into column 'Sessionid', table …
Dimo
  • 3,158
  • 6
  • 24
  • 45
19
votes
2 answers

Mapping many to many relationship in entity framework code first

I'm try make a test in EF, creating a many to many relationship, because I always mapping One to One or One to Many, I has get a example in the internet for try, the example is working for insert registers, but I can't read registers Here is my…
Lai32290
  • 6,540
  • 15
  • 52
  • 87
19
votes
5 answers

Performance for using 2 where clauses in LINQ

In LINQ-to-Entities you can query entities by doing: var students = SchoolContext.Students.Where(s => s.Name == "Foo" && s.Id == 1); I know that behind the scenes it will be translated to SQL to something similar to: SELECT * FROM Students WHERE…
rexcfnghk
  • 11,625
  • 1
  • 30
  • 55
19
votes
1 answer

System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'

I've been stuck trying to debug this issue. I believe the error Is occurring when I am trying to populate a drop down list. IEnumerable values = db.Customers.SqlQuery("SELECT * FROM Customer").ToList().Cast(); …
kayze
  • 678
  • 5
  • 17
  • 33
1 2 3
99
100