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
2
votes
2 answers

Ef core fluent api set all column types of interface

Unfortunately ef core does not support TPC-pattern, but we need this kind of behaviour. I´ve wrote an interface which is called IBase and each entity implements this interface: public interface IBase { Guid Id { get; set; } [Column(TypeName…
2
votes
1 answer

Sql multiple cascade paths/ Unable to determine the principal end of an association between the types entity framework code first

My issue is I keep getting this error using entity framework code first "Additional information: Introducing FOREIGN KEY constraint 'FK_dbo.Sets_dbo.ExerciseStats_ExerciseStatId' on table 'Sets' may cause cycles or multiple cascade paths. Specify ON…
JuniorPen
  • 21
  • 1
2
votes
2 answers

Invalid column name error from SQL generated by EF

Below you can see the SQL should join by using [ClassId1] instead of [Class1_ClassId] since the latter doesn't exist. I'm pretty sure I can use Fluent API to correct this but not sure what methods. Generated SQL SELECT ... FROM [dbo].[School] AS…
user3953989
  • 1,844
  • 3
  • 25
  • 56
2
votes
1 answer

How can I set a foreign key as primary key with Entity Framework?

I'm trying to set a foreign key as primary key using Entity Framework and fluent api. modelBuilder.Entity() .HasRequired(m => m.X) .WithRequireDependent(m => m.Y) .WillCascadeOnDelete(); My class: public class…
David J.E.
  • 358
  • 4
  • 14
2
votes
1 answer

entityframework core - many to many - The property expression is not valid

Application Version: Asp Net Core 1.1 EF: Microsoft.EntityFrameworkCore (1.1.1) Line: _dbContext.MyTable1.Include(c => c.MyIntermediateTable).ThenInclude(k => k.Select(x => x.MyTable2)).ToList(); Exception: The property expression 'k => {from…
2
votes
1 answer

How can I map a derived class having a collection of entities with Entity Framework

How can I map Dogs and Cats to have a collection of Toys but not Rats because they're not pets? public abstract class Animal { public int Id { get; set; } } public class Cat : Animal { public virtual ICollection Toys { get; set;…
Jamie Twells
  • 1,924
  • 4
  • 26
  • 56
2
votes
0 answers

ASP.NET Optional 1:1 relationship with ApplicationUser (Identity 2.0)

I have a Trainee which must connect to an ApplicationUser. The Trainee model has fields that do not relate to the actual user account, but needs to get things like FirstName, LastName from ApplicationUser as a user may not necessarily be a…
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
2
votes
2 answers

Fluent Api in Entity Framework 6 not compatible Entity Framework Core

I had implemented Fluent API using Entity Framework 6. Have problems when implementing the same using EnityFrameworkCore. Below is the code using Fluent API using EntityFramework 6 public class CustomerConfiguration :…
Tom
  • 8,175
  • 41
  • 136
  • 267
2
votes
1 answer

aspnet core entity framework 7 self referencing "job" 1 to many table

I have a "Job" table that contains jobs. The fact is Jobs are not always done in one go.. you can have a job that has many visits. I intended to represent that as another job but linked back to the original job via self referencing linkId. I am…
2
votes
2 answers

how to make 1:many relationship between models with different query ( linq syntax )

i have this : Models: { [Table("Requests")] public partial class RequestsModel { public RequestsModel() { this.CountView = new HashSet(); } [Key] public int Id {…
Arash Sh
  • 70
  • 1
  • 8
2
votes
1 answer

Mapping inheritance in EntityFramework Core

I'm using EntityFramework Core, Code First and Fluent Api to define model database, and i've follow situation about strategy of map inheritance: public class Person { public int Id { get; set; } public string Name { get; set;…
2
votes
1 answer

EF6 FluentAPI, 0:1 Unidirectional

I've spent the last three hours trying to figure this out and finally gave up (I'll work around it). But... just to be sure... is there no way to set up a unidirectional 0:1/1:1 in EF6 Fluent API? Consider: CREATE TABLE LegacyUsers ( ID INT NOT…
jleach
  • 7,410
  • 3
  • 33
  • 60
2
votes
1 answer

Make EF use a m:n table with additional property

I ran into a problem when creating a new app. The app is about new employees that enter on a specific date. As they need some Hardware (such as Notebooks, keyboards etc.) we want to provide an overview over all the new employees that are going to…
Th1sD0t
  • 1,089
  • 3
  • 11
  • 37
2
votes
0 answers

Navigation Property on Unique Key possible in Entity Core 1.0?

Is there a way to have navigation properties to Unique keys in EF Core? I tried "pretending" the unique key was a foreign key in the fluent api, but I got a runtime error (see comment). I know EF Core has Alternate Keys, but I'm not sure if…
2
votes
1 answer

Ef Code first Index with navigation property without adding Id

I have a EF Code first class called UserTag. UserTag has the following two properties public string TagText { get; set; } public UserGroup UserGroup { get; set; } Now I want an index that makes it so that a TagText is unique within a UserGroup. But…