का उपयोग करके संग्रह से किसी आइटम को निकालें I डीडीडी का उपयोग कर रहा हूं। मेरे पास एक वर्ग उत्पाद है जो एक समग्र रूट है।एंटिटी फ्रेमवर्क
public class Product : IAggregateRoot
{
public virtual ICollection<Comment> Comments { get; set; }
public void AddComment(Comment comment)
{
Comments.Add(comment);
}
public void DeleteComment(Comment comment)
{
Comments.Remove(comment);
}
}
मॉडल जो परत रखती है वह ईएफ के बारे में नहीं जानता है। समस्या यह है कि जब मैं DeleteComment(comment)
पर कॉल करता हूं, तो ईएफ अपवाद
'Product_Comments' एसोसिएशनसेट से एक संबंध 'हटाए गए' राज्य में है। बहुगुणित बाधाओं को देखते हुए, 'हटाए गए' राज्य में एक संबंधित 'product_Comments_Target' भी होना चाहिए।
भले ही तत्व संग्रह से हटा दिया गया हो, ईएफ इसे हटा नहीं देता है। डीडीडी तोड़ने के बिना इसे ठीक करने के लिए मुझे क्या करना चाहिए? (मैं टिप्पणियाँ रूप में अच्छी तरह के लिए भंडार बनाने की सोच रहा हूँ, लेकिन सही नहीं है)
कोड उदाहरण:
क्योंकि मैं DDD उपयोग करने के लिए कोशिश कर रहा हूँ, Product
एक समग्र जड़ है, और यह एक है भंडार IProductRepository
। एक टिप्पणी उत्पाद के बिना मौजूद नहीं हो सकती है, इसलिए Product
कुल बच्चे हैं, और Product
टिप्पणियां बनाने और हटाने के लिए ज़िम्मेदार है। Comment
में रिपोजिटरी नहीं है।
public class ProductService
{
public void AddComment(Guid productId, string comment)
{
Product product = _productsRepository.First(p => p.Id == productId);
product.AddComment(new Comment(comment));
}
public void RemoveComment(Guid productId, Guid commentId)
{
Product product = _productsRepository.First(p => p.Id == productId);
Comment comment = product.Comments.First(p => p.Id == commentId);
product.DeleteComment(comment);
// Here i get the error. I am deleting the comment from Product Comments Collection,
// but the comment does not have the 'Deleted' state for Entity Framework to delete it
// However, i can't change the state of the Comment object to 'Deleted' because
// the Domain Layer does not have any references to Entity Framework (and it shouldn't)
_uow.Commit(); // UnitOfWork commit method
}
}
ऐसा लगता है कि आप ईएफ के सेव चेंज को कॉल नहीं कर रहे हैं – Nagg
मुझे लगता है कि लक्ष्य नामक एक टेबल है। इस तालिका में टिप्पणियाँ तालिका के लिए एफके संदर्भ है। जब आप टिप्पणी तालिका में एक पंक्ति को हटाने का प्रयास करते हैं, तो लक्ष्य में सहयोगी पंक्तियों को पहले हटाने की आवश्यकता होती है। –
@Nagg मुझे यह त्रुटि मिलती है जब मैं SubmitChanges() – Catalin