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

Oracle.ManagedDataAccess.EntityFramework – ORA-01918: user ‘dbo’ does not exist

I had the same problem and it was resolved by Thiago Lunardi’s response. Thank you. I didn’t have enough reputation to vote up your response. To mention here, I succeeded after setting my schema name in UPPERCASE. Put this in your Context file under your new dbContext class, like this: public partial class MyAppContext : … Read more

Entity Framework CTP 4 – Code First Custom Database Initializer

I ran into the same problem. I didn’t really solve it, but I managed to get a little nasty workaround running, so i can deploy my solution to AppHarbor 😉 Its a IDatabaseInitializer implementation, that doesn’t delete the db, but just nukes all the constraints and tables, and then uses the ObjectContext.CreateDatabaseScript() method to generate … Read more

Understanding ForeignKey attribute in entity framework code first

The required side of the 1..0 relationship MemberDataSet should not have a FK to DeferredData. Instead, DeferredData‘s PK should also be a FK to MemberDataSet (known as shared primary key) public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual DeferredData DeferredData { get; set; } } public class DeferredData … Read more