in entity framework code first, how to use KeyAttribute on multiple columns

You can specify the column order in the attributes, for instance: public class MyEntity { [Key, Column(Order=0)] public int MyFirstKeyProperty { get; set; } [Key, Column(Order=1)] public int MySecondKeyProperty { get; set; } [Key, Column(Order=2)] public string MyThirdKeyProperty { get; set; } // other properties } If you are using the Find method of a … Read more

Does Entity Framework Code First support stored procedures?

EDIT: My original answer for EF4.1 (below) is now out of date. Please see the answer below from Diego Vega (who works on the EF team at Microsoft)! @gsharp and Shawn Mclean: Where are you getting this information? Don’t you still have access to the underlying ObjectContext? IEnumerable<Customer> customers = ((IObjectContextAdapter)this) .ObjectContext.ExecuteStoreQuery<Customer>(“select * from customers”); … Read more

Unique Constraint in Entity Framework Code First

As far as I can tell, there’s no way to do this with Entity Framework at the moment. However, this isn’t just a problem with unique constraints… you may want to create indexes, check constraints, and possibly triggers and other constructs too. Here’s a simple pattern you can use with your code-first setup, though admittedly … Read more

Entity Framework Code First – two Foreign Keys from same table

Try this: public class Team { public int TeamId { get; set;} public string Name { get; set; } public virtual ICollection<Match> HomeMatches { get; set; } public virtual ICollection<Match> AwayMatches { get; set; } } public class Match { public int MatchId { get; set; } public int HomeTeamId { get; set; } public … Read more

Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework

You can extract all the information from the DbEntityValidationException with the following code (you need to add the namespaces: System.Data.Entity.Validation and System.Diagnostics to your using list): catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation(“Property: {0} Error: {1}”, validationError.PropertyName, validationError.ErrorMessage); } } }

Why use ICollection and not IEnumerable or List on many-many/one-many relationships?

Usually what you choose will depend on which methods you need access to. In general – IEnumerable<> (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx) for a list of objects that only needs to be iterated through, ICollection<> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx) for a list of objects that needs to be iterated through and modified, List<> for a list of objects that needs … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)