Questions tagged [ef-fluent-api]

A way to configure Entity Framework beyond its conventions with a method chaining API

Use the tag for questions that ask about:

  • Model-wide settings
  • Property mapping
  • Type mapping
  • Relationships

The EF-Fluent API can be used in both c# and VB.NET.

The MSDN documentation

833 questions
9
votes
2 answers

Do I need to configure both sides of a relationship with Entity Framework with Fluent API?

I'm new to Fluent API. In my scenario, a Student can be in one Grade and a Grade can have many Students. Then, these two statements accomplish the same thing: modelBuilder .Entity() .HasRequired(s => s.Grade) .WithMany(s =>…
user11081980
  • 3,059
  • 4
  • 30
  • 48
9
votes
3 answers

Does the EF Fluent Api can setting the Minimum Length?

It seems use the Fluent API has more flexibility. So I choose the way to follow always use the Fluent API to determine the feature it have in db instead use the annotations to. But here is the problem,I couldn't find the way to set the Minimum…
葛厚德
  • 145
  • 3
  • 9
9
votes
2 answers

How to create spatial index using EF 6.1 fluent API

Well, the question is clear enough. Is it possible to create spatial indexes using Entity Framework 6.1 fluent API?
8
votes
1 answer

EF Core: Ignore property only on save

I have mapped an entity mapped to a database view (for querying data) and also to a table (for inserting, updating data). To map an entity to the database view, I use this code (from the EF 5.0 docs): modelBuilder .Entity() …
Joozef
  • 105
  • 2
  • 7
8
votes
3 answers

Entity Framework 7 Fluent API Doesn't Recognize IsOptional()

I'm currently setting up my database in my Asp.Net 5 project using entity framework 7, previously with EF 6, when I wanted to make some of my columns nullable, I would use: modelBuilder.Entity
().Property(t =>…
Hamid Mosalla
  • 3,279
  • 2
  • 28
  • 51
7
votes
1 answer

What is the benefit of adding .HasIndex() in your mappings, on a DBFirst scenario?

I have been searching on EF Core documentation, if adding .HasIndex() on your entities mappings would bring any benefits on a DbFirst scenario, and I couldn`t find anything. I have this 20yo DB that has all the necessary tables and indexes created,…
Tiago B
  • 1,937
  • 14
  • 34
7
votes
1 answer

EF Core: Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF on one to many

I am using EF Core together with ASP NET Core for my server, and when I am trying to update an existing value in the database I receive the following error: Cannot insert explicit value for identity column in table 'TeambuildingType' when…
Artyomska
  • 1,309
  • 5
  • 26
  • 53
7
votes
2 answers

How do I specify the foreign key in a one-to-one/zero relationship?

I have a parent entity and a child entity. In the DB the primary key for parent is p_p_id and the foreign key in the child is the same p_p_id There is no foreign key constraint in the database. The entities have the properties set up in their…
Smeegs
  • 9,151
  • 5
  • 42
  • 78
7
votes
3 answers

Set a constraint for minimum int value

I am using the Repository pattern. I have a entity called Product and I want to set the minimum value for price to avoid zero prices. Is it possible to create it in a EntitytypeConfiguration class? My product configuration class public class…
Rodrigão
  • 71
  • 1
  • 1
  • 5
7
votes
2 answers

One to one relationship with Entity Framework Fluent API

I'm having trouble with reverse navigation on one of my entities. I have the following two objects: public class Candidate { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long CandidateId { get; set; } .... //…
William
  • 75
  • 1
  • 1
  • 6
6
votes
2 answers

Fluent Api Entity Framework core

A user can have 1 or 0 account public class User { public int UserId { get; set; } public string Name { get; set; } public string Email { get; set; } public Account Account { get; set; } } public class…
Moth
  • 75
  • 1
  • 6
6
votes
2 answers

Entity-framework-7 Organizing Fluent API configurations into a separate class

I'm familiar how to organize the fluent API configurations into a separate class on EF6, but how is this achieved with EF7? Here is an example how to do it with EF6: ModelConfigurations.cs public class ModelConfigurations :…
ajr
  • 874
  • 2
  • 13
  • 29
6
votes
1 answer

TPC Inheritance Error

I've got an strange problem with TPC inheritance using C# Entity Framework Codefirst and Fluent Api. I have 3 Classes named Person, Invoice and PeriodicInvoice as you can see below. Here is a summary of my code: Invoice class and its configuration…
kianoosh
  • 610
  • 6
  • 22
6
votes
3 answers

Optional One-to-many Relationship in Entity Framework

I'm having issues getting an optional one-to-many relationship to work. My model is: public class Person { public int Identifier { get; set; } ... public virtual Department Department { get; set; } } public class Department { public…
givemelight
  • 103
  • 1
  • 1
  • 10
6
votes
1 answer

Entity Framework Code first: cycles or multiple cascade paths

I have a Booking class that has a booking contact (a Person) and a set of navigation properties (People) that links through a join table to another set of navigation properties (Bookings) in Person. How do I generate the Booking table with cascading…
James Peters
  • 73
  • 1
  • 1
  • 5
1 2
3
55 56