Questions tagged [ef-model-builder]

58 questions
24
votes
3 answers

EF Core: Using ID as Primary key and foreign key at same time

I have two entities, Prospect and Person, what I'm trying to do is use Prospect.ID as the primary key on Prospect table and as the foreign key of PersonID, my idea is use the same ID for both entities without the need of a PersonID on my Prospect…
Bruno Joaquim
  • 1,423
  • 1
  • 14
  • 15
19
votes
4 answers

How to disable model caching in Entity Framework 6 (Code First approach)

Following MSDN documentation we can read: The model for that context is then cached and is for all further instances of the context in the app domain. This caching can be disabled by setting the ModelCaching property on the given ModelBuidler, but…
Kryszal
  • 1,663
  • 14
  • 20
13
votes
1 answer

Can ASP.NET MVC + EF scaffolding be used after implementing EntityTypeConfiguration classes?

Background Visual Studio scaffolding for new ASP.NET MVC Controllers bound to Entity Framework work well when the models use data annotations or the direct lines within OnModelCreating(DbModelBuilder) to describe their…
10
votes
2 answers

EF Core move column data from table to another while migration (UP)

I updated my data model by removing a column from a table to another, but I hava data in this column To do that, I started by adding the new column in the new table and its done. Now I want to migrate column data from the old table to the new one,…
7
votes
3 answers

Resolving 'No key Defined' errors while using OnModelCreating with ApplicationDbContext?

I've been trying to create navigation properties for my collection types and I found this example of how one person accomplished it using OnModelCreating. I gave it a try in my MVC 5 application and I recieved this error when trying to update my…
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
5
votes
1 answer

EF Core 2.0 provider specific model configuration

I am trying to upgrade an EF Core 1.x project to 2.0. In 1.x I was taking advantage of provider-specific extension methods (e.g. ForSqlServerHasDefaultValueSql/ForSqliteHasDefaultValueSql or ForSqlServerHasColumnType) but these seem to have been…
lc.
  • 113,939
  • 20
  • 158
  • 187
5
votes
2 answers

EF migration is dropping a column and trying to rename a nonexistent column back to Id

I have a Code First MVC Project using EF. There are two tables, TableA and TableB, and they currently have a one-to-many relationship: public partial class TableA { public TableA() { TableBs = new HashSet(); } …
Landy
  • 177
  • 1
  • 13
5
votes
1 answer

Entity Framework Multiple Cascading Deletes

I'm developing an application using Entity-Framework code first and migrations. I have the following class, representing relation between two users when one user makes backup of another one for some product. User and Product classes don't have any…
Runaground
  • 83
  • 1
  • 8
4
votes
2 answers

What is the difference between the `Has...` and `ForSqlServerHas...` extension methods in EF7 property builder?

Entity Framework 7 (Entity Framework Core) has two different sets of extension methods on PropertyBuilder. The first seems to be the "generic" set: HasDefaultValueSql HasColumnName HasColumnType HasComputedColumnSql HasDefaultValue Then there is a…
lc.
  • 113,939
  • 20
  • 158
  • 187
2
votes
1 answer

EF Core: Optional one-to-one foreign key relation from content entity

I've implemented some database context for my .net core blazor application. The database context has access to an external database (no database migration etc.) Now my problem is that I am not sure how to define a foreign key using fluent api or…
2
votes
1 answer

Entity Framework DbContext ModelCaching property not available

I am writing some unit tests for Database creation using EF codefirst. During execution of Unit tests, the DBContext->OnModelCreating method is executed only 1 time, and the model is then cached for the rest of the remaining tests. I want to be able…
2
votes
0 answers

Generate Migration from Fluent API or ModelBuilder Conventions/Annotation (EF6)

Is there a way to generate the ForeignKey below in Fluent API on Model Mapping or ModelBuilder Conventions/Annotations? I'm using the EF6. I was able to create the needed foreign key changing manually after creating a migration! Works as expected...…
2
votes
2 answers

EF Code First One-to-One AND One-to-Many mapping

I have the following model and I am trying to construct a one-to-one and also one-to-many relationships from the same parent and child entities. The one-to-many relationship works with my current mappings but I am struggling with adding the new…
1
vote
2 answers

Do I need to index on a id in EF Core if I'm searching for an id in 2 different columns?

If I do a query like below where I'm searching for the same ID but on two different columns. Should I have an index like this? Or should I create 2 separate indexes, one for each column? modelBuilder.Entity() .HasIndex(p => new {…
chuckd
  • 13,460
  • 29
  • 152
  • 331
1
vote
1 answer

ModelBuilder' does not contain a definition for 'ApplyAllConfigurations'?

I am following this youtube video and at time 34:05, It is architecure for ASP.Net core using EntityFramework Core and they are showing an extension method modelBuilder.ApplyAllConfigurations(); I tried this in my code and it throws error:…
1
2 3 4