2010-07-17 44 views
0

我发现了一堆可能的重复,但没有一个真的似乎有我遇到的同样的问题。附:不能添加一个已经使用的密钥的实体

我得到一个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”作为第二个参数传递外)。

有谁知道这可能是什么原因造成的?

回答

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>(); 
} 
+0

这大致是我所做的。我实际上决定合并Images表并将其放入产品中以逗号分隔的列中。问题解决了。 :) – macca1 2010-07-24 16:57:59