Cho*_*mid 4 sql domain-driven-design mvvm sql-server-2008-r2 asp.net-mvc-4
我实际上在使用ASP.NET MVC4应用程序,我们使用实体框架和模型 - 视图 - 视图 - 模型方法和7层架构.我们有一个页面需要插入或更新"产品"信息.插入或更新的产品数据将保存在"产品"表中.我的数据库名称是"DbOnix".产品表的基本结构如下:
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
Run Code Online (Sandbox Code Playgroud)
Product表中的ProductCategoryID列与ProductCategory表具有外键关系.ProductCategory表的基本结构:
Column Name Data Type Allow Nulls
ProductCategoryID PK int
ProductCategoryName varchar(150) NO
Run Code Online (Sandbox Code Playgroud)
每当我尝试在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.
Run Code Online (Sandbox Code Playgroud)
我的控制器代码:
public HttpStatusCodeResult UpdateProductInformation(int id, ProductDTO ProductDTO)
{
_productManager.UpdateProductInformation(id, ProductDTO);
return new HttpStatusCodeResult(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
我的经理班级代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
我的核心(属性)类代码:
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; }
}
Run Code Online (Sandbox Code Playgroud)
和我的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; }
}
Run Code Online (Sandbox Code Playgroud)
请注意,我的数据库服务器是MS SQL Server 2008 R2.
编辑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"));
});
Run Code Online (Sandbox Code Playgroud)
请注意,我使用了KnockoutJS v2.3.0
在你的代码..
updatedProduct.ProductCategoryID = productDTO.ProductCategoryID;
Run Code Online (Sandbox Code Playgroud)
您可能正在分配ProductCategory表中不存在的值(ProductCategoryID) .因此,请检查您是否从数据库中获取了正确的ProductCategories(检查productDTO).问题可能是您的ProductCategoryID的值为0.这就是它说的原因UPDATE statement conflicted with the FOREIGN KEY constraint
| 归档时间: |
|
| 查看次数: |
14262 次 |
| 最近记录: |