Setting unique Constraint with fluent API?

I’m trying to build an EF Entity with Code First, and an EntityTypeConfiguration using fluent API. creating primary keys is easy but not so with a Unique Constraint. I was seeing old posts that suggested executing native SQL commands for this, but that seem to defeat the purpose. is this possible with EF6? 6 Answers … Read more

How to add/update child entities when updating a parent entity in EF

The two entities are one-to-many relationship (built by code first fluent api). public class Parent { public Parent() { this.Children = new List<Child>(); } public int Id { get; set; } public virtual ICollection<Child> Children { get; set; } } public class Child { public int Id { get; set; } public int ParentId { … Read more

Getting exact error type in from DbValidationException

I have the situation where I’m initializing my model in DatabaseInitializer() for EF 4.1 and get this annoying error “Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details.” So, I go to this EntityValidationErrors and there is a field {System.Data.Entity.Validation.DbEntityValidationResult} which gives me no information at all about what field it … Read more

Update Row if it Exists Else Insert Logic with Entity Framework [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 10 months ago. The community reviewed whether to reopen this question last month and left it closed: Original close reason(s) were not … Read more

Entity framework linq query Include() multiple children entities

This may be a really elementry question but whats a nice way to include multiple children entities when writing a query that spans THREE levels (or more)? i.e. I have 4 tables: Company, Employee, Employee_Car and Employee_Country Company has a 1:m relationship with Employee. Employee has a 1:m relationship with both Employee_Car and Employee_Country. If … Read more

The cast to value type ‘Int32’ failed because the materialized value is null

I have the following code. I’m getting error: “The cast to value type ‘Int32’ failed because the materialized value is null. Either the result type’s generic parameter or the query must use a nullable type.” when CreditHistory table has no records. var creditsSum = (from u in context.User join ch in context.CreditHistory on u.ID equals … Read more

ASP.NET Identity DbContext confusion

A default MVC 5 App comes with this piece of code in IdentityModels.cs – this piece of code is for all the ASP.NET Identity operations for the default templates: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base(“DefaultConnection”) { } } If I scaffold a new controller using views with Entity Framework and create … Read more

The object ‘DF__*’ is dependent on column ‘*’ – Changing int to double

Basically I got a table in my EF database with the following properties: public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } public string Image { get; set; } public string WatchUrl { get; set; } public int Year { get; set; } … Read more

What’s the difference(s) between .ToList(), .AsEnumerable(), AsQueryable()?

I know some differences of LINQ to Entities and LINQ to Objects which the first implements IQueryable and the second implements IEnumerable and my question scope is within EF 5. My question is what’s the technical difference(s) of those 3 methods? I see that in many situations all of them work. I also see using … Read more