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
6
votes
3 answers

EF Fluent API Many To Many with different ID Field Names

In this question: Ef Many To Many, an answer came up on how to manually specify a linking table. But I have a slightly unique situation (which I'm sure isn't really unique). My two tables each have an Id field. E.G.: [dbo].[Account].[Id] and…
Suamere
  • 5,691
  • 2
  • 44
  • 58
5
votes
1 answer

DeleteBehavior on Many-to-Many relationships in Entity Framework Core 5

Entity Framework Core 5 introduced Many-to-Many relationships without directly creating join tables. This is great and makes coding faster, but I have experienced some challenges. When working on relationships between two classes (student/teacher)…
5
votes
4 answers

CS0311 C# The type cannot be used as type parameter 'TContext' in the generic type or method. EntityFrameworkCore

I specify the context class I created in the entity project on the Startup.cs file and the connectionString data I created for connectionString. But why am I getting this error? ERROR message: Severity Code Description Project File Line …
el ahmadi
  • 51
  • 1
  • 1
  • 3
5
votes
1 answer

Case Insensitive Index mapping in EF Core

Is it possible to create a case insensitive unique index in Entity Framework core using Fluent API? For example, a case insensitive unique index can be defined in Oracle using the following SQL: create unique index test on…
5
votes
3 answers

Foreign key with constant in Fluent API

I need to load a reqired:many relationship from database. Now my problem is, that the the key of the related table consists of three keys: public partial class EnumValue { [Key] [Column(Order = 0)] [StringLength(14)] public string…
Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
5
votes
1 answer

How to specify a multi-column UNIQUE constraint in code-first Entity Framework fluent API

Suppose I have the following entities: public class Calendar{ public int ID{get;set;} public ICollection Days { get; set; } } public class Day{ public int ID{get;set;} public DateTime Date{get;set;} public int…
Matthew
  • 4,149
  • 2
  • 26
  • 53
5
votes
2 answers

How to dynamically change column name on model creating in Entity Framework context class

I have a base class like that: public class BaseClass : IEditableObject { public BaseClass() { } public Guid Id { get; set; } public void BeginEdit() { } public void CancelEdit() { } public void EndEdit() { …
test user
  • 55
  • 1
  • 6
5
votes
2 answers

Entity Framework 6 creates Id column even though other primary key is defined

I defined a DataObject as: public class SensorType : EntityData { //PKs public string CompanyId { get; set; } public string ServiceId { get; set; } public string Type { get; set; } } And used fluent API to make CompanyId and…
5
votes
2 answers

EF Code First Navigation Property to same table

I'm new to EF and struggling to implement the following scenario. I have an entity I'd like to have a navigation property to another of the same entity. E.g. public class Stage { public int ID { get; set; } public int? NextStageID { get;…
Kate
  • 1,556
  • 1
  • 16
  • 33
5
votes
1 answer

EF Fluent API: Set property for each entity derived from a base abstract class

I have a BaseClass, which is abstract, and has many abstract properties. I have a dozen or so (it will probably grow) entities that are part of the Entity Framework that each derive from BaseClass. I'm trying to avoid having to…
blgrnboy
  • 4,877
  • 10
  • 43
  • 94
5
votes
1 answer

Entity Framework - Code First - Ignore all properties except those specified

I am working with some large classes that have lots of properties, and I don't want to have to ignore all the properties I don't want to save to the database. Rather, is there anyway to ignore all properties and specify only the ones I want? So…
user3012633
  • 464
  • 5
  • 6
5
votes
1 answer

Entity framework mapping to view for read and to table for CUD

Lets assume I have and entity called Debt: public class Debt { [Key] public int Id { get; set; } public int Amount { get; set; } public int UserId { get; set; } } I'm using Code first, so I just simply introduce IDbSetand use…
5
votes
4 answers

How to add an index on multiple columns with ASC/DESC sort using the Fluent API?

I have a MVC ASP.NET application using Entity Framework 6 - Code First approach. Using the Fluent API, how can I add an index on multiple columns with ASC/DESC sort that is different for each column ? I've seen many examples using multiple columns…
5
votes
1 answer

How to create Lookup table and define relationships

As you can see at below, there are a Lookup table for the enum values and I want to create a relationship between a table's enum values and LookupKey column of the Lookup table (instead of ID column of the Lookup table). Lookup table: ID |…
5
votes
4 answers

EF Code First Fluent API define unique constraint

Im trying to enforce unique constraint of a column using fluent configuration for code first. Is there any better way than implementing it in business logic or using something like below context.ObjectContext.ExecuteStoreCommand("CREATE UNIQUE…
Geethanga
  • 1,836
  • 4
  • 20
  • 31