我有一個GenericService添加方法如下所示: -實體框架添加多個相關的實體與單個插入
public bool Add(T entity, Expression<Func<T, bool>> filter = null)
{
try
{
_genericRepository.Add(entity, filter);
}
catch (Exception e)
{
return false;
}
return true;
}
和GenericRepository添加方法如下所示: -
public void Add(T entity, Expression<Func<T, bool>> filter = null)
{
var existing = Get<T>(filter);
if (existing.Result != null) return;
Context.Add(entity);
Save();
}
這是呼叫我我正在ProductsController中執行: -
[HttpPost]
public IActionResult Create([FromBody] Product product)
{
if (product == null)
return BadRequest();
var result = _productsService.Add(product, m => m.Name == product.Name);
if (result)
{
return CreatedAtRoute("GetProducts", new { id = product.Id }, product);
}
return BadRequest("Item not added");
}
我正在通過集成測試的方式創建此文件作爲fo llows: -
testBrand = new Brand { Name = "testBrand" };
testImage = new Image { Name = "testImage", Url = "/Brands/adidas_logo_test.png" };
testBrand.Image = testImage;
testCategory = new Category {Name = "testCategory"};
testProduct = new Product
{
Category = testCategory,
Name = "testProduct",
Brand = testBrand,
BrandId = testBrand.Id,
CategoryId = testCategory.Id,
Deal = false,
Description = "testDescription",
Discount = "50% Discount",
Image = testImage,
ImageId = testImage.Id,
Price = new decimal(50.00),
Stock = 5
};
[Test]
public async Task Create_CreateAProduct_NewBrandNewCategoryNewImageProductsController()
{
//Arrange
//Act
//create new image
var requestImage = "api/Images/";
var postResponseImage = await _client.PostAsJsonAsync(requestImage, testImage);
var created = await postResponseImage.Content.ReadAsStringAsync();
var createdImage = JsonConvert.DeserializeObject<Image>(created);
//Act
testBrand.Image = createdImage;
testBrand.ImageId = createdImage.Id;
testImage.Id = createdImage.Id;
var postResponseProduct = await _client.PostAsJsonAsync(requestProduct, testProduct);
var createdProduct = await postResponseProduct.Content.ReadAsStringAsync();
var createdProductObj = JsonConvert.DeserializeObject<Product>(createdProduct);
var getResponse = await _client.GetAsync(requestProduct + "Get/" + createdProductObj.Id);
var fetched = await getResponse.Content.ReadAsStringAsync();
var fetchedProduct = JsonConvert.DeserializeObject<Product>(fetched);
// Assert
Assert.IsTrue(postResponseProduct.IsSuccessStatusCode);
Assert.IsTrue(getResponse.IsSuccessStatusCode);
Assert.AreEqual(testProduct.Name, createdProductObj.Name);
Assert.AreEqual(testProduct.Name, fetchedProduct.Name);
Assert.AreNotEqual(Guid.Empty, createdProductObj.Id);
Assert.AreEqual(createdProductObj.Id, fetchedProduct.Id);
}
一切工作正常,直到我嘗試插入具有多個相關實體的實體。讓我舉個例子。假設我有一個產品,它有一個FK ImageId,一個用於BrandId的FK和一個用於CategoryId的FK。 Brands實體已經爲Image實體提供了FK ImageId。
現在,當我嘗試插入新產品時,會插入2張圖片,一張來自品牌,另一張來自產品本身的圖片。所以在圖像表中,當我只需要一個新的產品圖像條目時,我會得到2個條目。此外,當我想要將現有圖像用於新產品時,這會造成問題。
所以我想創建一個新的服務/存儲庫的產品從通用服務/存儲庫繼承,並添加更多的邏輯。但是,有沒有更好的方法來做到這一點?
感謝您的幫助和時間
你在哪裏DbCOntextFIle和實體。藉助EF,您可以執行您想要的插入操作。 –
我的DBContext文件在Startup.cs(EF核心)中聲明,我的實體在Models文件夾中作爲類 – Johann
您可以顯示您爲'Brand'和'Product'分配'Image'的代碼,到你的「添加」方法? – Alisson