Questions tagged [fluent-entity-framework]

Fluent API alternative for assigning model relationships

Alternative to setting model relationships through annotations, Fluent API allows for a tighter, less ambiguous way to set relationships.

Overriding a DbContext's OnModelCreating Method, developers set required fields, primary and foreign keys, cascading delete relationships, etc...

23 questions
33
votes
2 answers

One to zero-or-one with HasForeignKey

I have two models: public class Person { public virtual int Id { get; set; } public virtual Employee Employee { get; set; } // optional } public class Employee { public virtual int Id { get; set; } public virtual int PersonId { get;…
joozek
  • 2,143
  • 2
  • 21
  • 32
20
votes
2 answers

Mapping foreign key in HasOptional().WithOptionalDependent() relation in Entity Framework 6

I have the following data-model in Entity Framework 6.1.3: using System.Data.Entity; public class Student { public int Id { get; set; } public virtual Contact Contact { get; set; } } public class Contact { public int Id { get; set; } …
Tom Pažourek
  • 9,582
  • 8
  • 66
  • 107
16
votes
4 answers

PropertyBuilder Does Not Contain A Definition For HasColumnType

I just built up a bunch of POCO classes and a DbContext class utilizing EntityFramework Core and the Scaffold-DbContext NuGet Package Manager Console command. It generated a bunch of code and most of it is fine, except there are several calls to…
KSwift87
  • 1,843
  • 5
  • 23
  • 40
5
votes
1 answer

EF Core fluent mapping to inner object properties

I have a class that contains some properties. For some architectural reasons, I have an instance of another objet into my class. Simple example public class MyEntity { public MySubEntity SubEntity {get; set;} } For this, I create fluent mapping…
cdie
  • 4,014
  • 4
  • 34
  • 57
4
votes
1 answer

EntityFramework Core Fluent Model Builder Key and Property

Ok so in entity framework 6 I would have had a key and property database generation in one statement: modelBuilder.Entity() .HasKey(x => x.Id) .Property(x => x.Id) …
4
votes
1 answer

Is there an EF6 DbContext Generator for C# which uses Fluent API?

I know that the EF6 VS Tools for Visual Studio 2012 come with a T4 template to generate DbContext classes which work with EF6. But I want to have a generator which uses fluent API. The older version I used with EF4 and EF5 don't work with EF6 and…
4
votes
1 answer

How to model one way one-to-one in Fluent EF?

Let's say I have the following entities: Box Id Crank crank // has one required relationship Crank Id // does not care about the box What is the proper way to define BoxMap? Is this sufficient? Or do I need WithRequiredPrincipal (which I…
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
2
votes
0 answers

Error in entity framework update with automapper. The role '...' of the relationship '...' has multiplicity 1 or 0..1

I'm trying to update a model that is getted by automapper bind. When my update function execute Context.Entry(obj).State = EntityState.Modified; entity framework throw this exception Multiplicity constraint violated. The role …
1
vote
1 answer

Mapping a private property as complex type in Entity Framework

Given this class: public class Basket { public int Id { get; set; } private string SomeProperty { get; set; } private Address Address { get; set; } public class BasketMapper : EntityTypeConfiguration { public…
mortenbock
  • 545
  • 5
  • 21
1
vote
0 answers

Ambient transaction across multiple contexts pointing to other databases

Short description of the setup: EF Core is used with fluent mapping. There are three databases and for each database one context. The problem is to begin a transaction scope across two or all the three contexts. The OnConfiguration method is…
1
vote
0 answers

Entity Framework 6 several one-to-mmay

Onother problem with Entity Framework 6. I don't know why, but I have some trouble to get back my object frome the database when I queue 2 one-to-many relationships. My plain objects public class Plan { public int id { get; set; } …
1
vote
1 answer

Entity Framework : how to create double relationship (1-1 and many-1) with same objects

I have two class : class Sub { public Guid Id { get; set; } public DateTime ValidDate { get; set; } public Guid MasterId { get; set; } public Master Master { get; set; } } and class Master { public Guid Id { get; set; } …
1
vote
0 answers

Multiple One-to-One Relationships of the Same Type on Entity in Entity Framework

I have a model: public class Foo { public virtual Bar Bar1 { get; set; } public virtual Bar Bar2 { get; set; } } public class Bar { public virtual Foo Foo { get; set; } } Bar1 and Bar2 are optional but Foo is required for all Bars.…
1
vote
0 answers

Representing a many-to-many relationship with lookup table in Entity Framework

I have a SQL database (which isn't normalized and doesn't have primary keys, only indexes which I cannot change the schema of) that has a many-to-many relationship. Reservations can have many Customers and vice-versa. The Reservation table is keyed…
1
vote
1 answer

How to create an optional foreign key to another table, using custom column names, with fluent api?

I'm using Entity Framework 5, and I am writing code-first models that I wish to use on top of an existing database schema. So, I have no control over the schema design. I have two tables, matters and users. The users table contains user accounts -…
Ryan
  • 867
  • 9
  • 23
1
2