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
1
vote
1 answer
ef core code first generic inheritance mapping
All Positions have a Location (Many-to-1).
Different Location types have different Position types
Model:
public abstract class Location
{
public int Id { get; set; }
public string Name { get; set; }
public int AreaId { get; set; }
…

JMH
- 13
- 4
1
vote
2 answers
Custom key with autoincrement
I have an entity with
entity.Property(f => f.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
because for some reason, I need to enter some id's manually.
is it possible to automatically add the next available id (int) if the id is not…

Demonia
- 332
- 3
- 14
1
vote
2 answers
Entity Framework: how to set the type of a column as uniqueidentifier with Fluent API
I use Entity Framework 6, code-first.
I have a column Id that is created by a framework (Azure Mobile Server SDK), which I don't have access to.
public abstract class EntityData
{
[Key]
[TableColumn(TableColumnType.Id)]
public…

Cristiano Ghersi
- 1,944
- 1
- 20
- 46
1
vote
1 answer
Entity Framework code first join 3 tables
I have my 3 classes:
public class ApplicationUser : IdentityUser
{
// all the default ApplicationUser properties etc...
public virtual ICollection UserShips { get; set; }
// other properties ...
}
public class Ship
{
…
user10022268
1
vote
0 answers
EF Map list property to single column in different table
I have the following situation:
I have a table 'Product' which has
Id | Name
Then I have another table Product_Vendor which has
Id | Product_Id | Vendor_Id
where Product_Id is a foreign key to Product.Id. Now in my Entity class code, I want the…

Jelle Capenberghs
- 794
- 2
- 14
- 30
1
vote
1 answer
How to insert data into pivot table in many to many relation in ASP.NET MVC & Entity Framework
I have two models, Movie and Genre.
Movie class:
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string File { get; set; }
…

Sujon Chondro Shil
- 105
- 1
- 2
- 10
1
vote
2 answers
How to correctly initialize many to many relationship with code-first
I'm trying to use code-first to model my db.
What I'm trying to map here is a 0 -> * relationship for Users and Challenges.
E.g A user can have 0 or more challenges associated with them. A challenge can have 0 or more users associated with…

TomSelleck
- 6,706
- 22
- 82
- 151
1
vote
1 answer
TimeStamp / Rowversion column with EF Core
I am trying to figure out how I should add a Timestamp/rowversion column to data tables in SQL Server via Entity Framework code first.
This is using EF Core V2.0.
I have tried two approaches to achieve this, the first:
public class Contact
{
…

Dom Sinclair
- 2,458
- 1
- 30
- 47
1
vote
1 answer
EF Code First access child of child to set ForeignKey
Below are the sample classes
public class Parent {
public int ParentId {get; set;}
public string UserName {get; set;}
public Child Child{get; set;}
}
public class Child {
public Child1[] ListChild1 {get; set;}
}
public class Child1…

Deepa
- 11
- 1
1
vote
2 answers
EF6 code-first getting a undesired referenceID
For some reason when generating my database I'm getting an unnecessary foreign key and I have no idea how to stop it.
A sample to replication this issue is as follow:
public class Person
{
public int PersonId { get; set; }
public string Name…

willthiswork89
- 617
- 1
- 4
- 17
1
vote
2 answers
loading related entities from .NET Core EF using Code First
I am trying to load related entities using .NET Core EF and Code First, however I am getting the following error :-
System.InvalidOperationException: The property 'Roles' is not a navigation property of entity type 'ApplicationUser'. The…

JMon
- 3,387
- 16
- 63
- 102
0
votes
0 answers
how to store foriegn key ids in new table in mvc in many to many relationship
the problem is that i have made three tables and first two table's primary keys are declared as foreign keys in the third table. now i want to store data in the third table but the problem is that no ids are being passsed to the foreign Key values…

Awais
- 1
- 1
0
votes
1 answer
EF Core - multiple parents with a list of the same child (code first)
Is there any way to represent something like this :
public abstract class DbEntity
{
[Key]
public string Id { get; set; } // <-- a GUID-String created by the domain
}
public class Carrier : DbEntity
{
public string Name { get; set; } =…

Caaged
- 1
- 3
0
votes
0 answers
Configuring cascade delete throws error - Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths
I have the following entities:
public class CompanyRelationshipEntity
{
public int ParentCompanyId { get; set; }
public int ChildCompanyId { get; set; }
public CompanyEntity? ParentCompany { get; set; }
public CompanyEntity?…

Mark Walsh
- 3,241
- 1
- 24
- 46
0
votes
1 answer
When using Entity Framework 6 Code-First approach, can I create index directly from database?
I have a recommendation from Azure to create the following index:
CREATE NONCLUSTERED INDEX [nci_wi_Transactions_71EA7D20388C3F47A9B9EBF2A1C7BA0C] ON [dbo].[Transactions] ([Client_Id]) INCLUDE ([Date], [Description], [Size], [Type]) WITH (ONLINE =…

arteny
- 339
- 1
- 4
- 14