我正在尝试将一个代理实体(一个联系人)添加到另一个代理实体(客户)的集合属性中。第collectionProperty.Add(entityModel);
行给出了错误
'System.Data.Objects.DataClasses.EntityCollection.Add(HTS.Models.Contact)'的最佳重载方法匹配' 有一些无效的论点
我在这里做错了什么?
public dynamic AddNew(
Type entityType,
string modelParentType,
int? modelParentId,
string collectionName
)
{
var parentType = Type.GetType("HTS.Models." + modelParentType);
var parentModel = Set(parentType).Find(modelParentId);
Set(parentType).Attach(parentModel);
var entityModel = Set(entityType).Create();
//var foreignKeyProperty = entityType.GetProperty(modelParentType + "Id");
//if (foreignKeyProperty != null)
//{
// foreignKeyProperty.SetValue(entityModel, modelParentId, null);
//}
var collectionProperty = (dynamic)parentType
.GetProperty(collectionName)
.GetValue(parentModel, null);
collectionProperty.Add(entityModel);
SaveChanges();
return entityModel;
}
用于生成错误的模型 - 请原谅所有注释,我还在学习。
public class Contact : SocialEntity {
public Contact() { Bag["DisplayName"] = "Contact"; }
[UIHint("hidden")]
public virtual int? CustomerId { get; set; }
[UIHint("NoEdit ManyToMany")]
public virtual ICollection<Customer> Customers { get; set; }
}
public class Customer : SocialEntity {
public Customer() { Bag["DisplayName"] = "Customer"; }
[Display(Name="Contacts", ShortName="Contact")] [UIHint("ManyToMany")]
public virtual ICollection<Contact> Contacts { get; set; }
//[Display(Name="Sales")] [UIHint("NoEdit")]
//public virtual ICollection<Sale> Sales { get; set; }
}
public class SocialEntity : BaseEntity {
[Display(Name="Name")]
public virtual string Name { get; set; }
[Display(Name="Addresses",ShortName="Address")]
public virtual ICollection<Address> Addresses { get; set; }
[Display(Name="PhoneNumbers",ShortName="PhoneNumber")]
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
[Display(Name="EmailAddresses",ShortName="EmailAddress")]
public virtual ICollection<EmailAddress> EmailAddresses { get; set; }
}
public class BaseEntity : Base { }
public class Base
{
public Base()
{
TypeName = this.GetType().BaseType.Name;
//EntityState = 16;
Bag = new Dictionary<string,string>();
Bag["AlignRight"] = "AlignRight";
Bag["DisplayName"] = "";
}
[UIHint("hidden")] [Display(Name="Id")]
public virtual int Id { get; set; }
[ScaffoldColumn(false)]
public virtual string empty { get; set; }
[NotMapped] [UIHint("hidden")]
public virtual string TypeName { get; set; }
[NotMapped] [UIHint("hidden")]
public virtual int? EntityState { get; set; }
[NotMapped] [ScaffoldColumn(false)]
public virtual bool? IsSavable { get; set; }
[NotMapped] [ScaffoldColumn(false)]
public virtual bool? Visited { get; set; }
[NotMapped]
[ScaffoldColumn(false)]
public virtual Dictionary<string,string> Bag { get; set; }
}
编辑:以下是调用AddNew()
public dynamic FindOrAddNew(
string entityType,
int? id,
string modelParentType,
int? modelParentId
)
{
var type = Type.GetType("HTS.Models." + entityType);
var parentType = Type.GetType("HTS.Models." + modelParentType);
dynamic entity = null;
if (id != null)
{
entity = Set(type).Find(id);
}
else
{
string collectionName = "";
ModelMetadata metadata = null;
foreach (PropertyInfo prop in parentType.GetProperties())
{
metadata = ModelMetadataProviders.Current
.GetMetadataForProperty
(
null, parentType, prop.Name
);
if (metadata.ShortDisplayName == entityType)
{
collectionName = prop.Name;
if (metadata.TemplateHint != null
&& metadata.TemplateHint.Contains("ManyToMany"))
{
entity = AddNew(type, modelParentType, modelParentId, collectionName);
}
else
{
entity = AddNew(type, modelParentType, modelParentId);
}
collectionName = "";
}
}
}
return entity;
}
编辑:以下是调用FindOrAddNew()
public ActionResult Edit(
string responseType,
string modelType,
int? modelId,
string modelPrefix,
string modelParentType,
int? modelParentId
)
{
if (modelType == null) // temporary initializer
{
modelType = "Customer";
modelId = 1;
}
var entity = db.FindOrAddNew(modelType, modelId, modelParentType, modelParentId);
SetSelectList(db, entity);
if (responseType == "Partial")
{
ViewData.TemplateInfo.HtmlFieldPrefix = modelPrefix;
ViewData["indexed"] = true;
entity.IsSavable = false;
return PartialView(entity);
}
else
{
entity.IsSavable = true;
return View(entity);
}
}
//SAVE
public void Save([ModelBinder(typeof(CustomModelBinder))] dynamic model)
{
db.Update(model, null, "");
db.SaveChanges();
}