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

Mapping to already existing database table

I am using Entity Framework 4.1 code first to connect to an already existing database. The table that I am using first is called Bank. I also have a Bank class as my domain model. This is how I mapped my class and table: public class HbfContext :…
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
4
votes
0 answers

Cascade deleting multiple tables

I have 4 tables. Cities, Providers, ProviderAdmins, AspNetUsers. I want to setup following behaviour: If i delete Cities entry - City was deleted. If i delete AspNetUsers entry - User was deleted -> ProviderAdmin was deleted If i delete…
4
votes
1 answer

Generating non-nullable rowversion on SQL Server with EF Core 3.1

We are trying to generate a non-nullable rowversion column on SQL Server with EF Core 3.1 using Fluent API: public class Person { public int Id { get; set; } public byte[] Timestamp { get; set; } } public class PersonEntityConfiguration :…
4
votes
2 answers

Duplicate foreign keys and columns being created for an id EF Core 3.1

Got a issue with ef core 3.1 creating duplicate columns for a id field from another table. i currently have a ApplicationUser entity that inherits from IdentityUser, and a property entity that stores an ApplicationUser id as UserId. public class…
Adam Wilson
  • 281
  • 2
  • 15
4
votes
0 answers

Custom DataAnnotations attribute to Fluent-API

I'm porting aproject from EF6 to EF7 Core and I'm converting all the classes with DataAnnotationtions to FluentAPI. In the project I have extensive use of Custom DataAnnotatiotions Attributes. I'm in the need to convert: [Required,…
4
votes
1 answer

SqlClient.SqlException: Invalid column name ClassNameId While fetching data from Fluent API

I am new to this EF,I am tring to fetch data from Sql Db table, and it gives me error like invalid classNameId error. public class ClassName { [Key] [Display(Name = "Id:")] public int Id { get; set; } [Display(Name =…
4
votes
1 answer

EF Core modelbuilder not a valid expression fluent api

When i try to do the command add-migration in my package manager I get the following error. The expression 'a => a.Customer' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'. This is…
Vito Mastroianni
  • 87
  • 1
  • 1
  • 7
4
votes
2 answers

EF Core Fluent API - unique constraint not being detected/included in migration

I am trying to add a unique constraint on two columns. I found multiple sources declaring that I should be using HasAlternateKey method for EF Core Fluent API applications. However, after running Add-Migration, the code that is generated in the…
4
votes
1 answer

Using Fluent Api to set foreign key constraint in Entity Framework Core 2.1

We're moving an existing application to .net core 2.1. We currently have a table called ApplicationUser which is referenced by most of the tables in our application because of the audit columns in the tables. Given the following…
Felix Cen
  • 733
  • 1
  • 6
  • 24
4
votes
1 answer

ForeignKey to the same table with custom column name

I have a model User : public class User { [Key] public int IDUser { get; set; } [Required] public string Forename { get; set; } [Required] public string Name { get; set; } public int?…
Dear Deer
  • 515
  • 1
  • 11
  • 29
4
votes
2 answers

ef core ignore navigation property

I have an entity User that has two properties CreatedBy and UpdatedBy both referencing User. By default, EF assumes that these two as a one to one relation to each other. I get the following error message: Message: System.InvalidOperationException…
4
votes
2 answers

EF Core - unique, indexed, auto-generated guid column that is not the primary key

I am migrating from .NET EF to EF CORE. I have a working solution that automatically generates a unique GUID aside from its primary key. Below is the solution. [Key, Column(Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] …
4
votes
2 answers

Define Entity Framework relationships using foreign keys only by FluentAPI

Is any way of defining Entity Framework relations using foreign keys only (no virtual properties of reference type) with FluentAPI (data models should not be changed)? CardDataModel public class CardDataModel { public int CardId { get; set;…
Alex Zaitsev
  • 2,544
  • 5
  • 18
  • 27
4
votes
1 answer

Entity Framework One-To-Many with only one navigation property: WithRequiredDependant?

Using the newest Entity Framework I have a class with a one-to-many wit only one navigation property on the many side. As stated in MSDN: Entity Framework Fluent API - Relationships: A one-directional (also called unidirectional) relationship is…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
4
votes
1 answer

One to One Relationship with Different Primary Key in EF 6.1 Code First

I am having an issue getting a reference to the employee object from the PayGroup object using Entity Framework 6.1. I have a foreign key in the database on PayGroup.SupervisorId -> Employee.EmployeeId. Note that this is a zero or one-to-one…