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
3
votes
1 answer

EF Core - Add constraint on multiple properties that one of them is required but not all

I have a class with some properties and I want to define a constraint on two properties (int type), that one of them is required but not both. In SQL it will like this: ALTER TABLE ADD CONSTRAINT CHECK ((
3
votes
1 answer

.Net Core Fluent API with Nullable references types

I can't seem to solve the following problem and can't find anything online that helps. I'm new to .Net Core and am also new to using nullable reference types and somehow I can't make them work together with EF Core. I have a data table that includes…
Mark
  • 93
  • 5
3
votes
1 answer

EF Core Add-Migration generating extra column with ColumnName1

I have the following entities when I generate migration it creates two columns with name RestrictedCategoryId and RestrictedCategoryId1(FK). How to solve this issue to generate only one column with FK? Note: I need OrderId in each entity. `C# public…
3
votes
1 answer

Is there a nice pattern for implementing an abstract DbContext in EF7 / .Net Core 3 to avoid duplication of shared entities / config across projects?

I have a number of different projects that all implement the same schema for configuration, security and audit and are looking for a pattern that would allow me to put these schema definitions in an abstract classes (entity, configuration and…
3
votes
2 answers

EF Core 2 - Relationship with only 1 navigation property/relationship knowledge?

I am using ASP.NET Core 2.2 with EF Core 2.2 and SQL server. I am currently working on a feature where I have the following classes: TABLE Foo ------------------------------------------------------------- Column Type …
3
votes
1 answer

The relationship from _ to _ is not supported because the owned entity type _ cannot be on the principal side of a non-ownership relationship

I'm having trouble with the entity framework core mappings. I get this exception 'The relationship from 'Payment' to 'Purchase.Payments' is not supported because the owned entity type 'Purchase' cannot be on the principal side of a non-ownership…
3
votes
2 answers

Entity Framework Core zero-or-one to zero-or-one relation

Given these classes: public class A { public int Id {get; set;} public int? BId {get; set;} public B B {get; set;} } public class B { public int Id {get; set;} public int? AId {get; set;} public A A {get; set;} } Then with…
Alex
  • 39
  • 1
  • 7
3
votes
0 answers

Entity Framework 6 fluent api delete cascade on n:n relationship

In my Entity Framework 6 code-first project, I have two tables with a many:many relationship. In Fluent API, I defined the relationship like this: modelBuilder.Entity() .HasMany(e => e.Recipes) .WithMany(e =>…
meppl
  • 73
  • 1
  • 8
3
votes
1 answer

DateTime.Now default value in entity configuration always sets the same DateTime in the database

I'm having a strange issue when configuring my entities in Fluent API using EF Core. All my entities have an EntityCreated field which is a DateTime object that is set to the current DateTime upon being added to the database as a new record. The…
Azhari
  • 535
  • 5
  • 20
3
votes
1 answer

EF Core one-to-many infinite includes

I have two tables Appointment and TaskAllocation having one to many relationship. now when i get the Appointment public IEnumerable GetAppointments(int employeeId, DateTime date) { return _context.Appointment.Where(a =>…
Noman Fareed
  • 274
  • 3
  • 11
3
votes
1 answer

Can EF Core configure a "real" One-To-One relation where both ends are required?

The EF Core documentation about One-To-One relations says: "When configuring the relationship with the Fluent API, you use the HasOne and WithOne methods." A closer look shows that this configures One-To-ZeroOrOne or ZeroOrOne-To-ZeroOrOne relations…
Gero Iwan
  • 73
  • 9
3
votes
1 answer

Fluent LINQ query with multiple join conditions, one of which being a simple equals and another a less than comparison between ids

I have a SQL query which includes a left join and a group by- so far so good- my trouble arises from one of the join conditions not being a straight "equals to" and I'm lost where to go with LINQ. I know multiple join conditions usually involves…
Shawson
  • 1,858
  • 2
  • 24
  • 38
3
votes
1 answer

Data Annotation or Fluent API? Best Practise for supporting various ORM's

I am looking into implementing a form a Data Validation on at the Data Layer of my application. I am looking to accept a specific string input into my data model, but am struggling to know how best to implement this. I am currently looking into…
3
votes
1 answer

Prevent index created by convention on many-to-many table

In the configuration below, EF creates an index on SyntaxId by convention. Since I have a composite primary key (serves as index) and no identity column, I do not think this convention-created index on a single column is needed in a many-to-many…
Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
3
votes
1 answer

Entity Framework Core fluent api One-To-Many and One-To-One produces duplicate foreign key

I'm changing my ASP.NET MVC project to ASP.NET Core MVC with Entity Framework Core and Fluent API. When I try to configure a one-to-one and one-to-many relationship, it generates duplicate foreign key columns in the dependent table. For example: I…