The relationship could not be changed because one or more of the foreign-key properties is non-nullable

I am getting this error when I GetById() on an entity and then set the collection of child entities to my new list which comes from the MVC view. The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a … Read more

DbEntityValidationException – How can I easily tell what caused the error?

I have a project that uses Entity Framework. While calling SaveChanges on my DbContext, I get the following exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details. This is all fine and dandy, but I don’t want to attach a debugger every time this exception occurs. More over, in … Read more

“A lambda expression with a statement body cannot be converted to an expression tree”

In using the EntityFramework, I get the error “A lambda expression with a statement body cannot be converted to an expression tree” when trying to compile the following code: Obj[] myArray = objects.Select(o => { var someLocalVar = o.someVar; return new Obj() { Var1 = someLocalVar, Var2 = o.var2 }; }).ToArray(); I don’t know what … Read more

MSSQL Error ‘The underlying provider failed on Open’

I was using an .mdf for connecting to a database and entityClient. Now I want to change the connection string so that there will be no .mdf file. Is the following connectionString correct? <connectionStrings> <!–<add name=”conString” connectionString=”metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQL2008;AttachDbFilename=|DataDirectory|\NData.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;” providerName=”System.Data.EntityClient” />–> <add name=”conString” connectionString=”metadata=res://*/conString.csdl|res://*/conString.ssdl|res://*/conString.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQL2008;Initial Catalog=NData;Integrated Security=True;Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;” … Read more

How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?

Let’s just suppose I have a valid need for directly executing a sql command in Entity Framework. I am having trouble figuring out how to use parameters in my sql statement. The following example (not my real example) doesn’t work. var firstName = “John”; var id = 12; var sql = @”Update [User] SET FirstName … Read more

Should services always return DTOs, or can they also return domain models?

I’m (re)designing large-scale application, we use multi-layer architecture based on DDD. We have MVC with data layer (implementation of repositories), domain layer (definition of domain model and interfaces – repositories, services, unit of work), service layer (implementation of services). So far, we use domain models (mostly entities) across all layers, and we use DTOs only … Read more