Let's say I have two models:

public class User
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
}

and

public class Friend
{
    [Key]
    public int FriendId { get; set; }
    public User A { get; set; }
    public User B { get; set; }
}

Let's say I only have 2 users in my database (ids: 1 (Jon) and 2 (Sam)). Now I insert into table friend like this:

db.Friends.Add(new Friend()
{
    A = db.Users.Find(1),
    B = db.Users.Where(u => u.UserId == 2).First()
});

db.SaveChanges();

Suddenly, I find a user (3, Sam) in a table user. What is the reasoning behind this? Not completely sure if relevant or not, but note that even if I make A and B fields virtual, nothing changes.

UPDATE

Finally found how to reproduce my problem. Apparently the problem isn't exactly the same as I described.

User a, b;

using (var db = new DbConnection())
{
    a = db.Users.First(u => u.UserId == 1);
    b = db.Users.First(u => u.UserId == 2);
}

using (var db = new DbConnection())
{
    db.Friends.Add(new Friend()
    {
        A = a,
        B = b
    });

    db.SaveChanges();
}

Now users will have 4 users. Does it mean that if I step out of transaction, I can no longer access the entities as if they were exactly the same items in the current transaction? Or maybe there is a way to make the program know that I am referring to the same item (because the ID is the same)?

有帮助吗?

解决方案

Honestly tried the same steps as you described and everything work well.. Anyway my steps

  1. 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)
        {
    
        }
    
    }
    
  2. 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();
    }
    
  3. I checked my db and found 2 records

  4. 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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top