FAQ Database Discussion Community


Set a constraint for minimum int value

c#,entity-framework,ef-code-first,ef-fluent-api
I am using the Repository pattern. I have a entity called Product and I want to set the minimum value for price to avoid zero prices. Is it possible to create it in a EntitytypeConfiguration class? My product configuration class public class ProductConfiguration : EntityTypeConfiguration<Product> { public PlanProductConfiguration(string schema =...

The relationship '{0}' was not loaded because the type '{1}' is not available

entity-framework,ef-fluent-api
I'm getting: The relationship 'Echipieri.Data.Event_Hosts' was not loaded because the type 'Echipieri.Data.User' is not available. I have found a few post about this problem but didn't find a solution for me. Here are my entities: public class User { public int ID { get; set; } public string Email {...

C# - EF 6 Relationships Fluent API (1:1..0)

c#,entity-framework,ef-code-first,foreign-keys,ef-fluent-api
I'm having some trouble finding out the correct way to map my Client/Person classes in the EF 6.1.3. public class Client { public int ClientId { get; set; } public int PersonId { get; set; } // FK // Navigation Properties public virtual Person Person { get; set; } }...

How to set default length on all fields in entity framework fluent api?

c#,.net,entity-framework,ef-fluent-api
Is it possible to set default length of all string fields (unless I say otherwise)? At the moment it really bugs me to go and add something like modelBuilder.Entity<Game>().Property(x => x.YardLine).HasMaxLength(256); to each and every string field, just to avoid having nvarchar(max) on all my strings columns in the database....

Code First Entity Framework 6: 1 to 1 with composite key

entity-framework,ef-code-first,entity-framework-6,ef-fluent-api
I've inherited a legacy SQL database that I am attempting to reverse engineer into Code First. Changing schema is not an option due to legacy software running on current config. What's strange to me is the developer configured the child table to have a composite primary key consisting of a...

One to zero or one relationship entity framework

c#,entity-framework,ef-code-first,ef-fluent-api
I am trying to enforce a one to (zero or one) table relationship using code first/fluent api and the intended table is as below. Student may only have a contact (StudentContact) or not But Every contact (StudentContact) must have a Student StudentID StudentName 1 StudentA 2 StudentB StudentContactID StudentContact StudentID...

System.NullReferenceException when Inserting a new item for many-to-many relationship in entity framework

c#,entity-framework,many-to-many,ef-fluent-api
When adding an item for many-to-many relationship, I receive this error An exception of type 'System.NullReferenceException' occurred in gEchoLu.dll but was not handled in user code with no further details. Here is the code that throws the exception: protected void btn_AddMembers_Click(object sender, EventArgs e) { gEchoLuDBContext _db = new gEchoLuDBContext();...

Entity Framework Code first: cycles or multiple cascade paths

c#,sql-server,entity-framework,ef-fluent-api
I have a Booking class that has a booking contact (a Person) and a set of navigation properties (People) that links through a join table to another set of navigation properties (Bookings) in Person. How do I generate the Booking table with cascading deletes enabled for the booking contact relationship?...

How to relate model property to same model in ASP.NET MVC?

asp.net-mvc,entity-framework,data-annotations,foreign-key-relationship,ef-fluent-api
Sometimes, you want to store who registered or created a user account. It's either the user registered himself/herself or some other user account registered him, such as Admin accounts. So, the User table would something like: public class User : Identity { public int Id { get; set; } public...

Entity Framework fluent api how to conditionally map multiple properties to a single table

c#,entity-framework,ef-fluent-api
Using fluent api, how do I map multiple properties of the same data type to a single table conditionally. Database Model: ListType would include the Grouping names i.e. Allergens ListItem would include all possible values for the given type ProductListItem contains all "selected" values for a given product. The goal...

Changing the Database Schema on a linking table (many-to-many) in EF

c#,entity-framework,ef-fluent-api
I am creating a many to many relationships with my EF Models. The linking table is being created which is fine, but it keeps putting it on the DBO Schema which I do not want. Yes, I could just modify the migration script after its generated but that should be...

Mapping from many-to-many in database to one-to-many in domain

.net,many-to-many,entity-framework-6,one-to-many,ef-fluent-api
I have a mapping challange in Entity Framework. In the database I have a many-to-many relation between two tables with a intermediate table with the PK of the both as bellow: I need to map this relation to a one-to-many domain entities relation as bellow: How can I map this...

How to solve multiple cascade paths with entity code first

c#,entity-framework-6,ef-fluent-api
A Template can have zero or many Document and zero or many MetaIndex. A Document can have zero or more Index, and a MetaIndex can also have zero or more Index: This represent a potential cascade problem because deleting a Template will delete its Document (which is OK); deleting a...