【发布时间】:2013-06-18 10:18:55
【问题描述】:
我有一个产品模型,允许用户为其产品定义自己的自定义字段。这是通过有 3 个表/模型来完成的。 Product 模型包含 CustomProductProperty 模型的 IEnumerable 枚举。在每个CustomProductPropertyModel 中都是DataType 模型的外键。这个DataType 模型是用户定义可用字段的地方。
我遇到的问题是,每次用户创建新产品时,都会创建一组新的DataType。如何处理数据以使 CustomProductProperty 指向现有的 DataType 而不是创建新的?
[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
if (ModelState.IsValid)
{
db.Products.Add(productViewModel.Product);
db.SaveChanges();
return RedirectToAction("Index");
}
编辑:创建产品的形式如下。
<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>
【问题讨论】:
-
您的数据类型必须附加到上下文中,否则将创建一个新的。
-
我该怎么做?
-
例如:
DataType dt = new DataType { CustomTypeID = someDataFromTheModel}; context.Set<DataType>().Attach(dt);。这假定 CustomTypeID 是 DataType 的主键。 -
如果我有一个包含 DataType 的模型的
ICollection<>集合,我是否只在foreach循环中运行它? -
是的,你必须这样做,或者你可以尝试附加一个包含 ICollection 的实体图。
标签: c# asp.net entity-framework asp.net-mvc-4