附加:无法添加具有已在使用的密钥的实体

时间:2010-07-17 18:49:50

标签: sql-server asp.net-mvc linq-to-sql

我发现了一堆可能的副本,但似乎没有一个我遇到同样的问题。

我收到DuplicateKeyException:无法添加一个已经在使用的密钥的实体。这是我的SqlProductsRepository:

public class SqlProductsRepository : IProductsRepository
{
    private Table<Product> productsTable;
    private Table<Image> imagesTable;

    public SqlProductsRepository(string connectionString)
    {
        productsTable = (new DataContext(connectionString)).GetTable<Product>();
        imagesTable = (new DataContext(connectionString)).GetTable<Image>();

        // Populate all of the images for each product found
        foreach (Image image in imagesTable)
        {
            productsTable.Where<Product>(x => x.ProductID == image.ProductID).FirstOrDefault().Images.Add(image);
        }
    }

    public IQueryable<Product> Products
    {
        get { return productsTable; }
    }

    public IQueryable<Image> Images
    {
        get { return imagesTable; }
    }

    public void SaveProduct(Product product)
    {
        EnsureValid(product, "Name", "Description", "Category", "Price");

        if (product.ProductID == 0)
            productsTable.InsertOnSubmit(product);
        else
        {            
            productsTable.Attach(product);

            productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
        }

        productsTable.Context.SubmitChanges();
    }

问题在于添加imagesTable。如果我为没有图像的产品执行此操作,则没有问题。只有当产品有图像时才会出现此问题。正如您所料,产品和图像非常基本:

[Table(Name = "Images")]
public class Image
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int ImageID { get; set; }

    [Column] public int ProductID { get; set; }
    [Column] public int SortOrder { get; set; }
    [Column] public string Path { get; set; }
}

[Table(Name = "Products")]
public class Product : IDataErrorInfo
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync=AutoSync.OnInsert)]
    public int ProductID { get; set; }

    [Column] public string Name { get; set; }
    [Column] public string Description { get; set; }
    [Column] public decimal Price { get; set; }
    [Column] public string Category { get; set; }

    private IList<Image> _images;
    public IList<Image> Images {
        get
        {
            if (_images == null)
                _images = new List<Image>();
            return _images;
        }
        set
        {
            _images = value;
        }
    }

这个问题正在扼杀我。我尝试了各种各样的变化,包括首先获取原始产品并将其作为Attach的第二个参数传递(除了仅将“true”作为第二个参数传递)。

有谁知道可能导致这种情况的原因?

1 个答案:

答案 0 :(得分:0)

首先观察。假设您使用LINQ to SQL标记,请尝试使用单个数据上下文和LoadOptions。像(未经测试)的东西:

private Table<Product> productsTable;
private Table<Image> imagesTable;
private DataContext context;

public SqlProductsRepository(string connectionString)
{
    context = new DataContext(connectionString);
    var loadOptions = new DataLoadOptions();
    loadOptions.LoadWith<Product>(item => item.Image);
    context.LoadOptions = loadOptions;
    productsTable = context.GetTable<Product>();
}