无法在对象'dbo.User'中插入重复键。\\\<br/>n该语句已被终止 [英] Cannot insert duplicate key in object &#39;dbo.User&#39;.\r\nThe statement has been terminated

查看:144
本文介绍了无法在对象'dbo.User'中插入重复键。\\\<br/>n该语句已被终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户表。对于诸如CreatedBy之类的字段,其他表格中引用了这个表。



问题是,当我插入另一个表的行(比如'x')时尝试将新用户插入到用户表中。



应该做的是将CreatedBy作为现有用户插入到表'x'中。



使用实体框架4.任何人面临过这样的问题?

解决方案

您可以将实体与相关实体一起插入,也可以插入没有相关实体的实体,仅引用现有实体。这取决于你写的代码。



示例1:

  User user = GetUserFromSomewhere(); 
using(var context = new MyContext())
{
Order order = new Order();
order.CreatedBy = user;

context.Orders.AddObject(order);
//将两个订单和相关实体用户放入添加状态
//,因为用户没有附加到上下文

context.SaveChanges();
//创建新订单和新用户并设置它们之间的关系
}

示例2:

  using(var context = new MyContext())
{
User user = context.Users.SingleOrDefault(u => u.Id == 1);
//查询将此用户附加到此上下文
//用户处于状态现在不变

订单= new Order();
order.CreatedBy = user;

context.Orders.AddObject(order);
//将订单置于已添加状态,但不会触及已附加的相关实体的
//状态 - >用户保持
//在状态Unchanged

context.SaveChanges();
//引用用户创建新订单,但不创建新用户
}

示例3:

  User user = GetUserFromSomewhere(); 
using(var context = new MyContext())
{
context.Users.Attach(user);
//我们明确地附加于告知EF的上下文
//我们知道该用户存在于DB
//用户处于状态不变现在

订单订单=新订单();
order.CreatedBy = user;

context.Orders.AddObject(order);
//将订单置于已添加状态,但不会触及已附加的相关实体的
//状态 - >用户保持
//在状态Unchanged

context.SaveChanges();
//引用用户创建新订单,但不创建新用户
}

修改



示例4:

  int userId = GetUserIdFromSomewhere(); 
using(var context = new MyContext())
{
var user = new User {Id = userId};
//我们使用正确的主键
//创建一个存根用户实体//没有必要设置其他属性
//只设置与订单
$ b的关系$ b context.Users.Attach(user);
//我们明确地附加于告知EF的上下文
//我们知道该用户存在于DB
//用户处于状态不变现在

订单订单=新订单();
order.CreatedBy = user;

context.Orders.AddObject(order);
//将订单置于已添加状态,但不会触及已附加的相关实体的
//状态 - >用户保持
//在状态Unchanged

context.SaveChanges();
//引用用户创建新订单,但不创建新用户
}


I have a user table. there are references to this table from other tables for fields such as CreatedBy.

Problem is, when I am inserting a row of another table (say 'x'), it tries to insert a new user into the user table.

What it should be doing is insert a row into table 'x' with CreatedBy as the existing user.

Using Entity Framework 4. Anyone faced a problem like that before?

解决方案

You can insert an entity together with related entities or you can insert an entity without the related entities, just referencing existing ones. It depends on the code you write.

Example 1:

User user = GetUserFromSomewhere();
using (var context = new MyContext())
{
    Order order = new Order();
    order.CreatedBy = user;

    context.Orders.AddObject(order);
    // will put both order and related entity user into Added state
    // because user is not attached to the context

    context.SaveChanges();
    // creates new order and new user and sets the relationship between them
}

Example 2:

using (var context = new MyContext())
{
    User user = context.Users.SingleOrDefault(u => u.Id == 1);
    // query attaches this user to this context
    // user is in state Unchanged now

    Order order = new Order();
    order.CreatedBy = user;

    context.Orders.AddObject(order);
    // will put the order into Added state but doesn't touch the
    // state of already attached related entities -> user remains
    // in state Unchanged

    context.SaveChanges();
    // creates new order with reference to user, but doesn't create new user
}

Example 3:

User user = GetUserFromSomewhere();
using (var context = new MyContext())
{
    context.Users.Attach(user);
    // we attach explicitely to the context telling EF thereby
    // that we know that this user exists in the DB
    // user is in state Unchanged now

    Order order = new Order();
    order.CreatedBy = user;

    context.Orders.AddObject(order);
    // will put the order into Added state but doesn't touch the
    // state of already attached related entities -> user remains
    // in state Unchanged

    context.SaveChanges();
    // creates new order with reference to user, but doesn't create new user
}

Edit

Example 4:

int userId = GetUserIdFromSomewhere();
using (var context = new MyContext())
{
    var user = new User { Id = userId };
    // we create a stub user entity with the correct primary key
    // It's not necessary to set other properties
    // to only set the relationship to the order

    context.Users.Attach(user);
    // we attach explicitely to the context telling EF thereby
    // that we know that this user exists in the DB
    // user is in state Unchanged now

    Order order = new Order();
    order.CreatedBy = user;

    context.Orders.AddObject(order);
    // will put the order into Added state but doesn't touch the
    // state of already attached related entities -> user remains
    // in state Unchanged

    context.SaveChanges();
    // creates new order with reference to user, but doesn't create new user
}

这篇关于无法在对象'dbo.User'中插入重复键。\\\<br/>n该语句已被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆