Honestly tried the same steps as you described and everything work well.. Anyway my steps
Created a db context class derived from `DbContext'
public class EFContext : DbContext { public DbSet<Friend> Friends { get; set; } public DbSet<User> Users { get; set; } public EFContext(string connectionString) : base(connectionString) { } }
I use MSQL2008 Express with win auth so I created the Users table
using (var db = new EFContext(@"Data Source=yourMachineName\SQLEXPRESS2008;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True")) { db.Users.Add(new User() { UserId = 1, Name = "John" }); db.Users.Add(new User() { UserId = 2, Name = "Sam" }); db.SaveChanges(); }
I checked my db and found 2 records
After I created the Friends table
using(var db = new EFContext(@"Data Source=yourMachineName\SQLEXPRESS2008;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True")) { db.Friends.Add(new Friend() { A = db.Users.Find(1), B = db.Users.Where(u => u.UserId == 2).First() }); db.SaveChanges(); }
Again I got 1 record in the Friends table with columns FriendId=1, A_UserId=1, B_UserId=2. I checked the Users table and I still have 2 records.
If I were you I would try my code in a separate app. If it works then please post here all steps which led you to this problem.