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

750 questions
197
votes
3 answers

How to do a join in linq to sql with method syntax?

I have seen lots of examples in LINQ to SQL examples on how to do a join in query syntax but I am wondering how to do it with method syntax? For example how might I do the following var result = from sc in enumerableOfSomeClass join soc…
chobo2
  • 75,304
  • 170
  • 472
  • 780
188
votes
6 answers

Setting unique Constraint with fluent API?

I'm trying to build an EF Entity with Code First, and an EntityTypeConfiguration using fluent API. creating primary keys is easy but not so with a Unique Constraint. I was seeing old posts that suggested executing native SQL commands for this, but…
kob490
  • 2,913
  • 4
  • 19
  • 36
122
votes
1 answer

Entity Framework Code First - Advantages and disadvantages of Fluent Api vs Data Annotations

When creating a database using Entity Framework code-first, a lot of the database model is can be extracted from the code. Fluent API and/or Attributes can be used to fine tune the model. What are the advantages and disadvantages of Fluent Api in…
Sam
  • 14,131
  • 23
  • 79
  • 144
79
votes
7 answers

One to one optional relationship using Entity Framework Fluent API

We want to use one to one optional relationship using Entity Framework Code First. We have two entities. public class PIIUser { public int Id { get; set; } public int? LoyaltyUserDetailId { get; set; } public LoyaltyUserDetail…
52
votes
5 answers

What is Entity Framework fluent api?

I keep hearing about the Entity Framework fluent-api but I am struggling to find a good reference on this. What is it? We use the entity framework and the modeling tool provided. Is that all that is? Or is it something different? Similarly, if it's…
Chev
  • 54,842
  • 60
  • 203
  • 309
44
votes
3 answers

Fluent API, many-to-many in Entity Framework Core

I've searched stackoverflow for a proper solution on generating a many-to-many relationship, using EF Core, Code first and Fluent API. A simple scenario would be: public class Person { public Person() { Clubs = new HashSet(); …
Anonymous
  • 1,123
  • 3
  • 15
  • 29
34
votes
1 answer

How to define Many-to-Many relationship through Fluent API Entity Framework?

Below is my model: public class TMUrl { //many other properties //only property with type Keyword public List Keywords{get;set;} } public class Keyword { //many other properties //only property with type TMUrl …
Manish Mishra
  • 11,383
  • 5
  • 23
  • 52
32
votes
4 answers

EntityFramework Code First FluentAPI DefaultValue in EF6.X

How can I set the default value using EntityFramework Code First FluentAPI for bool property? Something like: Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);
Tony Bao
  • 872
  • 2
  • 12
  • 22
25
votes
2 answers

Entity Framework Linq query: .Where chain vs &&

This question pertains to query optimization using Linq with the Entity Framework. Is there any difference between chaining .Where clauses and using && in a single .Where clause of a linq query with the entity framwork? E.g.: suppose I have the…
bunglestink
  • 768
  • 7
  • 16
18
votes
4 answers

Map System.Uri using Entity Framework Fluent Api

Pretty simple question. I have a model that has a property which is a System.Uri type. Uris don't have a default parameterless constructor, and no ID field. Is there any way to override my model generation to store it in the DB in a custom way…
Paul
  • 32,974
  • 9
  • 79
  • 112
17
votes
1 answer

Entity Framework Core 2.0: How to configure abstract base class once

I have a base model: public abstract class Status { public string updateUserName { get; set; } } Then a model which extends the base model defined above: public class Item : Status { public int Id { get; set; } public string…
17
votes
4 answers

Use IEntityTypeConfiguration with a base entity

In EF Core 2.0, we have the ability to derive from IEntityTypeConfiguration for cleaner Fluent API mappings (source). How can I extend this pattern to utilize a base entity? In the example below, how can I have a BaseEntityConfiguration to reduce…
17
votes
3 answers

Generate a composite unique constraint/index, in EF Core

I need a composite unique constraint for my entity's Name property, which is unique per Category (for which it has an FK). So something like this: entityTypeBuilder .HasIndex(i => new { i.Name, i.Category.Id }) .IsUnique(); But this fails when…
17
votes
1 answer

Multiple indexes possible using HasColumnAnnotation?

It looks like in Entity Framework 6.1 they added the ability to create table indexes via the new HasColumnAnnotation method. I created a few helper extensions to speed up the process: public static class MappingExtensions { public static…
Sam
  • 9,531
  • 9
  • 60
  • 100
16
votes
4 answers

Set EF6 Code First strings fluently to nvarchar(max)

I'm building an EF6 code first model using the fluent API. My understanding is, by default, strings will be nvarchar(max), which (to be blunt) is dumb for a default. So I added the following convention code to set max default length to 255…
Jeremy Holovacs
  • 19,993
  • 29
  • 99
  • 234
1
2 3
49 50