在每次创建产品时都将创建重复的数据类型 [英] Duplicate DataType is being created on every Product Creation
问题描述
我有一个产品模型,允许用户为他们的产品定义自己的自定义"字段.这可以通过3个表/模型来完成. Product
模型,其中包含CustomProductProperty
模型的IEnumerable枚举.每个CustomProductPropertyModel
中的DataType
是DataType
模型的外键.此DataType
模型是用户定义可用字段的地方.
I have a Product Model that allows users to define their own Custom fields for their Product. This is accomplished by have 3 tables/models. The Product
model which contains an IEnumerable enumeration of CustomProductProperty
models. in each CustomProductPropertyModel
is a foreign key to a DataType
model. This DataType
model is where users define what fields are available.
我遇到的问题是,每当用户创建一个新产品时,就会创建一组新的DataType
.如何处理数据,以使CustomProductProperty
指向现有的DataType
而不创建新的DataType
?
The problem I am having is that everytime a user creates a new Product, a new set of DataType
is created. How do I handle the data so that The CustomProductProperty
points to an existing DataType
and not creating a new one?
[Table("Product")]
public class Product
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID { get; set; }
public int ProductTypeID { get; set; }
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[ForeignKey("ProductTypeID")]
[Display(Name = "Product Type")]
public virtual ProductType ProductType { get; set; }
public virtual ICollection<CustomProductProperty> CustomProperties { get; set; }
}
[Table("CustomProductProperty")]
public class CustomProductProperty
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomPropertyID { get; set; }
public int CustomDataTypeID { get; set; }
[ForeignKey("CustomDataTypeID")]
public CustomDataType DataType { get; set; }
public int ProductID { get; set; }
[ForeignKey("ProductID")]
public virtual Product Product { get; set; }
public string PropertyValue { get; set; }
}
[Table("CustomDataType")]
public class CustomDataType
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomTypeID { get; set; }
public string PropertyName { get; set; }
public CustomDataTypes DataType { get; set; }
public int ModuleID { get; set; }
[ForeignKey("ModuleID")]
public Module Module { get; set; }
}
在创建表单中,我为DataType.CustomTypeID
添加了@Html.HiddenFor()
,并在调试时包含了它,但是当我添加到dbcontext中时,它会创建一个全新的DataType
In the creation form I included a @Html.HiddenFor()
for DataType.CustomTypeID
and when debugging, it is included but when I add to my dbcontext, it creates a brand new DataType
if (ModelState.IsValid)
{
db.Products.Add(productViewModel.Product);
db.SaveChanges();
return RedirectToAction("Index");
}
用于创建产品的表单如下.
The form for creating a product is as follows.
<table class="basic_info_tbl">
<tr>
<th>@Html.LabelFor(model => model.Product.ProductName):</th>
<td>@Html.TextBoxFor(model => model.Product.ProductName) </td>
<th>@Html.LabelFor(model => model.Product.ProductType):</th>
<td>@Html.DropDownListFor(model => model.Product.ProductTypeID, Model.ProductTypes)</td>
@Html.EditorFor(model => model.Product.CustomProperties)
</tr>
</table>
使用编辑器模板:
<tr>
@Html.HiddenFor(model => model.DataType.CustomTypeID)
<th>@Model.DataType.PropertyName</th>
<td>@Html.TextBoxFor(model => model.PropertyValue)</td>
</tr>
推荐答案
来自Lerman&米勒的书 DbContext :
From Lerman & Miller's book DbContext:
添加图的根将导致 要在上下文中注册为新实体的图形.这种行为是一样的 如果您使用
DbSet.Add
或将实体的State
属性更改为Added
.一旦所有 实体由状态管理器跟踪,您可以然后围绕图表进行操作, 为每个实体指定正确的状态.
adding the root of a graph will cause every entity in the graph to be registered with the context as a new entity. This behavior is the same if you use
DbSet.Add
or change theState
property for an entity toAdded
. Once all the entities are tracked by the state manager, you can then work your way around the graph, specifying the correct state for each entity.
(我的重点)
因此,在db.Products.Add(productViewModel.Product)
之后,对象图中的CustomDataType
对象也是Added
.
So after db.Products.Add(productViewModel.Product)
also the CustomDataType
objects in the object graph are Added
.
Add()
之后,您应该遍历对象图
After the Add()
you should loop through the object graph
foreach (var dt in productViewModel.Product.CustomProperties
.Select(x => x.DataType).ToList())
{
Entry(dt).State = EntityState.Unchanged;
}
这篇关于在每次创建产品时都将创建重复的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!