通过使用 EF 插入数据库对多个实例的实体引用
entity referenced to multiple instances by instert into the database with EF
我将使用 Entity Framework 和 ASP.NET MVC 应用程序将博客插入到我的数据库中。如果我想将实体添加到上下文中,我在下面代码的最后一行遇到了这个错误。
InvalidOperationException
: An entity object cannot be referenced by multiple instances of IEntityChangeTracker
.
这是我的代码:
public class AdminController : Controller
{
[HttpPost]
public ActionResult CreateBlog(BlogViewModel vm)
{
try
{
_blogService.Insert(new Blog()
{
Titel = vm.Titel,
Beschrijving = vm.Beschrijving,
Content = (vm.Actie.ToLower() == "publiceer" ? true : false),
Verwijderd = false,
Auteur = UserManager.FindById(User.Identity.GetUserId())
});
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return RedirectToAction(nameof(Blog));
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public AdminController(ApplicationUserManager userManager, IBlogService blog)
{
UserManager = userManager;
_blogService = blog;
}
}
public class BlogService : IBlogService
{
public void Insert(Blog blog)
{
_blogRepo.Insert(blog);
_blogRepo.SaveChanges();
}
}
public class BlogRepo : GenericRepo<Blog>
{
public override Blog Insert(Blog blog)
{
context.Entry(blog.Auteur).State = EntityState.Unchanged;
return dbSet.Add(blog); // --> on this line
}
}
你能找到我做的问题吗?提前致谢。
我想你的实体模型看起来像:
public class Blog
{
[Key]
public int Id { get; set; }
[Required]
// etc
public string Titel { get; set; }
public string Beschrijving { get; set; }
public bool Content { get; set; }
public bool Verwijderd { get; set; }
public ApplicationUser Auteur { get; set; }
}
为什么不直接将 AuteurId 属性 添加到您的实体 class。通过这种方式,您将避免在获取用户实体 (UserManager.FindById(User.Identity.GetUserId())
).
时进行额外查询
public class Blog
{
[Key]
public int Id { get; set; }
[Required]
// etc
public string Titel { get; set; }
public string Beschrijving { get; set; }
public bool Content { get; set; }
public bool Verwijderd { get; set; }
[ForeignKey("Auteur")]
public string AuteurId { get; set; }
public virtual ApplicationUser Auteur { get; set; }
}
然后在您的操作代码中将像
_blogService.Insert(new Blog()
{
Titel = vm.Titel,
Beschrijving = vm.Beschrijving,
Content = (vm.Actie.ToLower() == "publiceer" ? true : false),
Verwijderd = false,
AuteurId = User.Identity.GetUserId()
});
我将使用 Entity Framework 和 ASP.NET MVC 应用程序将博客插入到我的数据库中。如果我想将实体添加到上下文中,我在下面代码的最后一行遇到了这个错误。
InvalidOperationException
: An entity object cannot be referenced by multiple instances ofIEntityChangeTracker
.
这是我的代码:
public class AdminController : Controller
{
[HttpPost]
public ActionResult CreateBlog(BlogViewModel vm)
{
try
{
_blogService.Insert(new Blog()
{
Titel = vm.Titel,
Beschrijving = vm.Beschrijving,
Content = (vm.Actie.ToLower() == "publiceer" ? true : false),
Verwijderd = false,
Auteur = UserManager.FindById(User.Identity.GetUserId())
});
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
return RedirectToAction(nameof(Blog));
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public AdminController(ApplicationUserManager userManager, IBlogService blog)
{
UserManager = userManager;
_blogService = blog;
}
}
public class BlogService : IBlogService
{
public void Insert(Blog blog)
{
_blogRepo.Insert(blog);
_blogRepo.SaveChanges();
}
}
public class BlogRepo : GenericRepo<Blog>
{
public override Blog Insert(Blog blog)
{
context.Entry(blog.Auteur).State = EntityState.Unchanged;
return dbSet.Add(blog); // --> on this line
}
}
你能找到我做的问题吗?提前致谢。
我想你的实体模型看起来像:
public class Blog
{
[Key]
public int Id { get; set; }
[Required]
// etc
public string Titel { get; set; }
public string Beschrijving { get; set; }
public bool Content { get; set; }
public bool Verwijderd { get; set; }
public ApplicationUser Auteur { get; set; }
}
为什么不直接将 AuteurId 属性 添加到您的实体 class。通过这种方式,您将避免在获取用户实体 (UserManager.FindById(User.Identity.GetUserId())
).
public class Blog
{
[Key]
public int Id { get; set; }
[Required]
// etc
public string Titel { get; set; }
public string Beschrijving { get; set; }
public bool Content { get; set; }
public bool Verwijderd { get; set; }
[ForeignKey("Auteur")]
public string AuteurId { get; set; }
public virtual ApplicationUser Auteur { get; set; }
}
然后在您的操作代码中将像
_blogService.Insert(new Blog()
{
Titel = vm.Titel,
Beschrijving = vm.Beschrijving,
Content = (vm.Actie.ToLower() == "publiceer" ? true : false),
Verwijderd = false,
AuteurId = User.Identity.GetUserId()
});