Я фактически работаю над приложением ASP.NET MVC4, где мы использовали инфраструктуру сущности и подход Model-View-View-Model и 7-слойную архитектуру. У нас есть одна страница, где нам нужно вставить или обновить информацию о продукте. Введенные или обновленные данные продукта будут сохранены в таблице «Продукт». Имя моей базы данных - «DbOnix». Базовая структура продукции Таблица приведена ниже:Операция UPDATE конфликтует с ограничением FOREIGN KEY в ASP.NET MVC4
Column Name Data Type Allow Nulls
ProductID PK int
ProductName varchar(255) NO
ProductCategoryID FK int
Sequence int YES
ActiveStatus int YES
SlNo int NO
В столбце ProductCategoryID в таблице Product имеет отношение внешнего ключа с таблицей ProductCategory. Основная структура ProductCategory таблицы:
Column Name Data Type Allow Nulls
ProductCategoryID PK int
ProductCategoryName varchar(150) NO
Всякий раз, когда я пытаюсь вставить или обновить данные в таблице Product, следующее исключение:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Product_ProductCategory". The conflict occurred in database "DbOnix", table "dbo.ProductCategory", column 'ProductCategoryID'.The statement has been terminated.
My Controller Код:
public HttpStatusCodeResult UpdateProductInformation(int id, ProductDTO ProductDTO)
{
_productManager.UpdateProductInformation(id, ProductDTO);
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
Мой код Класс менеджер:
public void UpdateProductInformation(int id, ProductDTO productDTO)
{
//if productDTO data is not valid
if (productDTO == null)
throw new ArgumentException(Messages.warning_CannotAddProfileWithNullInformation);
//Create a new product entity
var currentProduct = _ProductRepository.Get(id);
var updatedProduct = new Product();
updatedProduct.ProductID = id;
updatedProduct.ProductName = productDTO.ProductName;
updatedProduct.ProductCategoryID = productDTO.ProductCategoryID;
updatedProduct.Sequence = productDTO.Sequence;
updatedProduct.ActiveStatus = productDTO.ActiveStatus;
updatedProduct.SlNo = productDTO.SlNo;
//Update Product
updatedProduct = this.UpdateProduct(currentProduct, updatedProduct);
}
Мой основной (Свойство) Класс Код:
public partial class Product : Entity, IValidatableObject
{
public Product()
{
}
[Key]
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
public int Sequence { get; set; }
public int ActiveStatus { get; set; }
public int SlNo { get; set; }
}
и мой DTO Код класса:
public class ProductDTO
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int ProductCategoryID { get; set; }
public int Sequence { get; set; }
public int ActiveStatus { get; set; }
public int SlNo { get; set; }
}
Обратите внимание, что мой сервер базы данных является MS SQL Server 2008 R2.
EDIT-1: Я забыл включить свой код JavaScript:
$(function() {
var Product = function (Product) {
var self = this;
self.ProductID = ko.observable(Product ? Product.ProductID : 0).extend({ required: true });
self.ProductName = ko.observable(Product ? Product.ProductName : '').extend({ required: true });
self.ActiveStatus = ko.observable(Product ? Product.ActiveStatus : 0);
};
var ProductCollection = function() {
var self = this;
//if ProfileId is 0, It means Create new Profile
if (ProductID == 0) {
self.Product = ko.observable(new Product());
}
else {
$.ajax({
url: urlProduct + '/GetProductById/' + ProductID,
async: false,
dataType: 'json',
success: function (json) {
self.Product = ko.observable(new Product(json));
}
});
}
self.ProductErrors = ko.validation.group(self.Product());
self.saveProduct = function() {
var isValid = true;
if (self.ProductErrors().length != 0) {
self.ProductErrors.showAllMessages();
isValid = false;
}
if (isValid) {
self.Product().ActiveStatus = document.getElementById("stat").value;
$.ajax({
type: (ProductID > 0 ? 'PUT' : 'POST'),
cache: false,
dataType: 'json',
url: urlProduct + (ProductID > 0 ? '/UpdateProductInformation?id=' + ProductID : '/SaveProductInformation'),
data: JSON.stringify(ko.toJS(self.Product())),
contentType: 'application/json; charset=utf-8',
async: false,
success: function (data) {
alert("Product saved successfully.");
window.location.href = '/Product';
},
error: function (err) {
var err = JSON.parse(err.responseText);
var errors = "";
for (var key in err) {
if (err.hasOwnProperty(key)) {
errors += key.replace("Product.", "") + " : " + err[key];
}
}
$("<div></div>").html(errors).dialog({ modal: true, title: JSON.parse(err.responseText).Message, buttons: { "Ok": function() { $(this).dialog("close"); } } }).show();
},
complete: function() {
}
});
}
};
};
var ProductsViewModel = function() {
var self = this;
var url = "/Product/GetAllProduct";
var refresh = function() {
$.getJSON(url, {}, function (data) {
self.Products(data);
});
};
// Public data properties
self.Products = ko.observableArray([]);
// Public operations
self.createProduct = function() {
window.location.href = '/Product/ProductCreateEdit/0';
};
self.editProduct = function (product) {
//alert(product.ProductID);
window.location.href = '/Product/ProductCreateEdit/' + product.ProductID;
};
};
ko.applyBindings(new ProductsViewModel(), document.getElementById("productlist"));
ko.applyBindings(new ProductCollection(), document.getElementById("product_edit"));
});
Обратите внимание, что я использовал KnockoutJS v2.3.0
Вы уверены, что 'productDTO.ProductCategoryID' в' UpdateProductInformation (int id, ProductDTO productDTO) 'имеет правильное значение? Кажется, что вы пытаетесь ввести не существовавший «ProductCategoryID». –
Как сказал Роман Изосимов, возможно, вы пытаетесь добавить «Продукт» с «ProductCategoryID», который еще не существует в таблице «ProductCategory». – user2989408
О добавлении нового продукта, да, кажется, что нулевое значение сохраняется в столбце ProductCategoryID, потому что я фактически не объявлял никаких свойств в файле Javascript. Но пока я пытаюсь обновить существующий продукт, определенная ценность в таблице ProductCategoryID определенно. –