我正在尝试创建集成测试。我有三个表一个是产品,另一个是制造商,另一个是product_manufacturer的映射表。
当我尝试创建产品然后分配制造商时,我必须在我的情况下传递productId productId是自动递增的,我不知道如何传递它。现在我的问题是如何以最佳方式实现此问题的解决方案。
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
Category_Product_SpecificationAttribute_Mapping = new HashSet<Category_Product_SpecificationAttribute_Mapping>();
Product_Category_Mapping = new HashSet<Product_Category_Mapping>();
Product_SpecificationAttribute_Mapping = new HashSet<Product_SpecificationAttribute_Mapping>();
Product_Manufacturer_Mapping = new HashSet<Product_Manufacturer_Mapping>();
Product_Picture_Mapping = new HashSet<Product_Picture_Mapping>();
ProductPriceHistories = new HashSet<ProductPriceHistory>();
ProductReviews = new HashSet<ProductReview>();
ProductTags = new HashSet<ProductTag>();
}
public int Id { get; set; }
[Required]
[StringLength(400)]
public string Name { get; set; }
public string ShortDescription { get; set; }
public string FullDescription { get; set; }
public string AdminComment { get; set; }
public bool ShowOnHomePage { get; set; }
[StringLength(400)]
public string MetaKeywords { get; set; }
public string MetaDescription { get; set; }
[StringLength(400)]
public string MetaTitle { get; set; }
public bool AllowCustomerReviews { get; set; }
public int ApprovedRatingSum { get; set; }
public int NotApprovedRatingSum { get; set; }
public int ApprovedTotalReviews { get; set; }
public int NotApprovedTotalReviews { get; set; }
public bool MarkAsNew { get; set; }
public DateTime? MarkAsNewStartDateTimeUtc { get; set; }
public DateTime? MarkAsNewEndDateTimeUtc { get; set; }
public int DisplayOrder { get; set; }
public bool Published { get; set; }
public bool Deleted { get; set; }
public DateTime CreatedOnUtc { get; set; }
public DateTime UpdatedOnUtc { get; set; }
public int ClickCount { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Category_Product_SpecificationAttribute_Mapping> Category_Product_SpecificationAttribute_Mapping { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product_Category_Mapping> Product_Category_Mapping { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product_SpecificationAttribute_Mapping> Product_SpecificationAttribute_Mapping { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product_Manufacturer_Mapping> Product_Manufacturer_Mapping { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Product_Picture_Mapping> Product_Picture_Mapping { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductPriceHistory> ProductPriceHistories { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductReview> ProductReviews { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductTag> ProductTags { get; set; }
}
[TestMethod]
public void InsertProduct_Manufacturer_Graph()
{
using (var dataContext = new PriceHunterDataContext())
using (var unitOfWork = new UnitOfWork(dataContext))
{
var manufacturerRepository = new Repository<Manufacturer>(dataContext, unitOfWork);
var manufacturer = manufacturerRepository.Query(x => x.Name == "Apple").Select().FirstOrDefault();
var name = "Test Product";
var product = new Product
{
Name = name,
ObjectState = ObjectState.Added,
Product_Manufacturer_Mapping = new[]
{
new Product_Manufacturer_Mapping()
{
ProductId = 1,//here is the issue. It is auto increment key
ManufacturerId = manufacturer.Id,
IsFeaturedProduct = true,
DisplayOrder = 1,
},
}
};
var productRepository = new Repository<Product>(dataContext, unitOfWork);
productRepository.InsertOrUpdateGraph(product);
unitOfWork.SaveChanges();
var insertedpermission = productRepository.Query(x => x.Name == name).Select().FirstOrDefault();
Assert.IsNotNull(insertedpermission);
Assert.IsTrue(insertedpermission.Name == name);
Assert.AreEqual(name, insertedpermission.Name);
}
}