Relating to using the fluent mapping API of Entity Framework Code first for configuring the SQL that gets generated on running migrations.
Questions tagged [ef-code-first-mapping]
93 questions
13
votes
1 answer
How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC
I'm new to Entity Framework code-first. This is my learning in ASP.NET MVC, using code-first for database creation.
I have two classes:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int…

Raida Adn
- 405
- 1
- 6
- 17
8
votes
5 answers
How to setup entity relations in Entity Framework Core
Here we are on EF Core and got 3 tables:
News
Items
Links
And more (except News, Items): Content, Post, Form, etc.
And my model definitions
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public decimal…

Alex
- 311
- 2
- 14
5
votes
1 answer
asp.net core create a constraint on two columns, one of which is another entity
I have 2 classes (entities) Exemption and Period, and I want to make a unique index with IdNumber from exemption and Id from Period
public class Exemption
{
[Key]
public int Id { get; set; }
…

Osama
- 325
- 3
- 14
4
votes
1 answer
How to make Entity Framework Core create a foreign key constraint that is not enforced
I'm using code-first with EF Core 3.1.2
I have established the following two-way one-to-one relationship in the OnModelCreating method of my DbContext.
modelBuilder.Entity()
.HasOne(i => i.SpecialDefect)
.WithOne(d =>…

tassieboy
- 160
- 1
- 1
- 11
4
votes
1 answer
Set default value for multiple columns in a single statement
I'm using EF Core Code-First and need to set default value for multiple columns.
I know this is the statement for setting default for one column
modelBuilder.Entity()
.Property(b => b.AdminFee)
…

FLICKER
- 6,439
- 4
- 45
- 75
4
votes
1 answer
EF code first migrations from Pascal Case Properties to lower case columns
In Entity Framework 6 Code First, is there a way (perhaps via Data Annotations or Fluent API) so that the database generated by Migrations has lower case column names, even though my model classes have Pascal Casing properties?
I.e. This class:
…

Fabricio Rodriguez
- 3,769
- 11
- 48
- 101
3
votes
2 answers
It is possible to query a NotMapped property?
i'm working with EF6 code first, and i used this answer to map a List in my entitie.
This is my class
[Key]
public string SubRubro { get; set; }
[Column]
private string SubrubrosAbarcados
{
get
{
…

Juan Salvador Portugal
- 1,233
- 4
- 20
- 38
2
votes
1 answer
ASP.NET Core Entity Framework code-to-database scaffolding not recognizing Id property or [Key] annotation
I'm trying to scaffold out a razor page set for my Ticket class and I'm getting an error when Entity Framework tries to create the database:
My understanding is that if you have a property "Id" it will use it, and if you set the [Key] annotation it…
2
votes
0 answers
How would someone generate documentation from an Entity Framework code first mapping context?
I would like to generate a document from the Entity Framework code-first context class. The documentation would generate an object reference model and how it is mapped to the database. How would one go about doing this as part of the build process?

David Good
- 21
- 2
2
votes
2 answers
getting an error if mapping is not there with EF core code first
Hi all i have model sections like this below
public class Sections
{
public int SectionId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Requests Requests { get; set; }
}
the…

Glory Raj
- 17,397
- 27
- 100
- 203
2
votes
1 answer
EF Core Deleting related entities on One To One relationship
I have a one to one relationship set up on ef core. When i try to delete Article
entity I need to cascade MediPlan since it is one to one relationship. When I Delete Article, MediaPlan is not getting removed.
Here is set up.
public class Article
{
…

kkdeveloper7
- 497
- 2
- 11
- 25
2
votes
3 answers
EF Core - error with duplicated key while adding data with unique index
I'm trying to add data to the database using EF Core, but I cannot overcome duplicate key error: Cannot insert duplicate key row in object 'dbo.Stocks' with unique index 'IX_Stocks_Name'. The duplicate key value is (stock1).
I have entity Stock that…

Łukasz Sypniewski
- 602
- 1
- 10
- 19
2
votes
1 answer
How to extract a part of Property configuration in EF6 Code First to reuse several times?
To map a column in EF6 Code First, we use this code for example :
Property(o => o.Email).HasColumnType("varchar").HasMaxLength(255).IsRequired();
To prevent to write several times ".HasColumnType("varchar").HasMaxLength(255)" for all Email columns…

Phil
- 1,035
- 2
- 10
- 17
2
votes
2 answers
Change table name "__MigrationsHistory"?
Is it possible to change table name "__MigrationsHistory" when using codefIrst?
Problem: I am using Oracle Database and I have rules to create new tables. One of them is that there can not be any table names or fields with special characters.
2
votes
2 answers
DBContext Method not found (System.MissingMethodException on OnModelCreating())
I've been stuck with below exception from OnModelCreating() in DBContext and struggling to find the cause or a solution. Can't find much help online either.
{System.MissingMethodException: Method not found: 'System.Nullable`1…

Sudarshan Tanuku
- 191
- 1
- 11