EF4 Code First: how to add a relationship without adding a navigation property

I believe you always need navigation property on at least one side when using code-first. Then you will be able to map it: public class User { public string UserId { get; set; } public string PasswordHash { get; set; } public bool IsDisabled { get; set; } public DateTime AccessExpiryDate { get; set; } … Read more

Self-referencing many-to-many recursive relationship code first Entity Framework

By convention, Code First will take uni-directional associations as one to many. Therefore you need to use fluent API to let Code First know that you want to have a many to many self referencing association: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Member>().HasMany(m => m.Friends).WithMany().Map(m => { m.MapLeftKey(“MemberId”); m.MapRightKey(“FriendId”); m.ToTable(“MembersFriends”); } ); }

SQL Server Express connection string for Entity Framework Code First

The problem with your connection string here is: <add name=”TrempimModel” connectionString=”data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.sdf; User Instance=true” providerName=”System.Data.SqlClient” /> You’re basically defining what “server” you’re connecting to – but you’re not saying what database inside the file to connect to. Also – the file extension for SQL Server Express database files is .mdf (not .sdf – … Read more

Entity Framework – Is there a way to automatically eager-load child entities without Include()?

No you cannot do that in mapping. Typical workaround is simple extension method: public static IQueryable<Car> BuildCar(this IQueryable<Car> query) { return query.Include(x => x.Wheels) .Include(x => x.Doors) .Include(x => x.Engine) .Include(x => x.Bumper) .Include(x => x.Windows); } Now every time you want to query Car with all relations you will just do: var query = … 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

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

Entity Framework DateTime and UTC

Here is one approach you might consider: First, define this following attribute: [AttributeUsage(AttributeTargets.Property)] public class DateTimeKindAttribute : Attribute { private readonly DateTimeKind _kind; public DateTimeKindAttribute(DateTimeKind kind) { _kind = kind; } public DateTimeKind Kind { get { return _kind; } } public static void Apply(object entity) { if (entity == null) return; var properties = … Read more