实体框架核心更新多对多对象

时间:2016-06-06 16:14:02

标签: c# asp.net-core entity-framework-core .net-core-rc2

我有这个代码来更新实体

 public async Task<User> UpdateAsync(User entity)
 {
      var userDb = await _context.User.Include(x => x.UserApplications).ThenInclude(xx =>
      xx.Application).Include(x => x.State).Include(x => x.UserProfiles).ThenInclude(x => 
      x.Profile).FirstOrDefaultAsync(x => x.UserName == entity.UserName && x.SecretKey == 
      entity.SecretKey && x.Email == entity.Email);


    userDb.FirstName = entity.FirstName;
    userDb.LastName = entity.LastName;
    userDb.State = await _context.State.FirstAsync(x => x.StateId == entity.State.StateId);

    userDb.UserProfiles.Clear();
    userDb.UserApplications.Clear();
    userDb.UserProfiles.AddRange(entity.UserProfiles); //ManyToMany
    userDb.UserApplications.AddRange(entity.UserApplications); //Many To Many
    _context.User.Update(userDb);
    await _context.SaveChangesAsync();
    return userDb;
}

生成sql

  

执行DbCommand(8ms)[参数= [@ p0 =&#39;?&#39;,@ p1 =&#39;?&#39;,@ p14 =&#39;?&#39; ,@ p2 =&#39;?&#39;,@ p3 =&#39;?&#39;,@ p4 =&#39;?&#39;,@ p5 =&#39;?&# 39;,@ p6 =&#39;?&#39;,@ p7 =&#39;?&#39;,@ p8 =&#39;?&#39;,@ p9 =&#39;? &#39;,@ p10 =&#39;?&#39;,@ p11 =&#39;?&#39;,@ p12 =&#39;?&#39;,@ p13 =&#39 ;?&#39;,@ p15 =&#39;?&#39;,@ p16 =&#39;?&#39;,@ p17 =&#39;?&#39;,@ p18 =& #39;?&#39;,@ p19 =&#39;?&#39;,@ p20 =&#39;?&#39;,@ p21 =&#39;?&#39;],CommandType =&#39;文字&#39;,CommandTimeout =&#39; 30&#39;]插入&#34;应用程序&#34; (&#34; ApplicationId&#34;,&#34; ApplicationName&#34;)VALUES(@ p0,@ p1);更新&#34;用户&#34; SET&#34; ApiKeyId&#34; = @ p2,&#34;评论&#34; = @ p3,&#34; CreationDate&#34; = @ p4,&#34;电子邮件&#34; = @ p5,&#34; FirstName&#34; = @ p6,&#34; IsOnLine&#34; = @ p7,&#34; LastLoginDate&#34; = @ p8,&#34;姓氏&#34; = @ p9,&#34;密码&#34; = @ p10,&#34; SecretKey&#34; = @ p11,&#34; StateId&#34; = @ p12,&#34;用户名&#34; = @ p13 WHERE&#34; UserId&#34;与@ p14不相同; DELETE FROM&#34; UserApplication&#34;在哪里&#34; UserApplicationId&#34;与@ p15不相同;删除&#34; UserProfile&#34;在哪里&#34; UserProfileId&#34;从@ p16开始并不是很明显;删除&#34; UserProfile&#34;在哪里&#34; UserProfileId&#34;与@ p17不同; INSERT INTO&#34; UserProfile&#34; (&#34; ProfileId&#34;,&#34; UserId&#34;)VALUES(@ p18,@ p19)RETURNING&#34; UserProfileId&#34 ;; INSERT INTO&#34; UserProfile&#34; (&#34; ProfileId&#34;,&#34; UserId&#34;)VALUES(@ p20,@ p21)RETURNING&#34; UserProfileId&#34 ;;

为什么EF插入新实体

  ( INSERT INTO "Application" ("ApplicationId", "ApplicationName")
  VALUES (@p0, @p1)) if only i need update the user?
  • EF Core版本:1 RC2
  • 操作系统:Mac OSX

此致

用户类

public sealed class User
{
    public int UserId { get; set; }
    public string SecretKey { get; set; }
    public ApiKey Apikey { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Comment { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime LastLoginDate { get; set; } = DateTime.Now;
    public DateTime CreationDate { get; set; } = DateTime.Now;
    public bool IsOnLine { get; set; } = false;
    public State State { get; set; } = new State();
    public List<UserProfile> UserProfiles { get; set; } = new List<UserProfile>();
    public List<UserApplication> UserApplications { get; set; } = new List<UserApplication>();


}

UserApplication Class

 public class UserApplication
{
    public int UserApplicationId { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
    public int ApplicationId { get; set; }
    public Application Application { get; set; }
}

2 个答案:

答案 0 :(得分:1)

致电时

    userDb.UserApplications.Clear();

你清除每个UserApplication,这可能是Application的主体。级联删除正在删除Application,因此EF知道必须重新插入它。

答案 1 :(得分:0)

我很久以前就遇到过这种情况。

在我的情况下,插入正在发生,因为我添加的对象具有主键(Id)= 0.因此EF总是被理解为新行。

可能是一种可能性。

相关问题