我有一个控制器方法,我正在尝试保存在视图中编辑过的表单。
[HttpPost]
public ActionResult ModifyContract(ModContract mod)
{
// submit modified contract
if (ModelState.IsValid)
{
OutlookMediaEntities1 db = new OutlookMediaEntities1();
db.ObjectStateManager.ChangeObjectState(mod, EntityState.Modified);
// save changes
db.SaveChanges();
return RedirectToAction("ContractDetails", "Contract", new { id = (int) ViewData["contractid"] });
}
else
{
ModelState.AddModelError("", "Missing necessary information");
return View();
}
}
我在db.ObjectStateManager ...行上遇到InvalidOperationException。我看到可能是因为我没有分配主键并注意到我的ModAds类(下面)没有包含主键。添加它但我收到同样的错误。我尝试在我之前的问题中按照回答者的建议做一个db.Attach(mod),但是我得到了一个编译器错误。 Attach将“IEntityWithKey”作为参数。所以显然我的实体没有钥匙,所以我认为我要宣布模型的东西一定是错的。
public class ModContract
{
public int contract_id; // primary key?
public string contract_name { get; set; }
public List<ModAds> ads;
public string print_product_id { get; set; }
public string print_ad_option_id { get; set; }
}
public class ModAds
{
public int contr_ad_id; // primary key?
public string name;
public string product_name;
public string adv_product;
public List<string> editions;
public double freq_disc;
public double other_dis_dol;
public double? other_dis_per;
public string non_cash_note;
public double non_cash_cons;
}
有没有办法指定主键是什么?我的模型有点奇怪,因为我在ModContract中有一个ModAd列表,你可以看到。不确定这可能是一个问题。此外,我尝试将getter和setter添加到所有字段以查看是否存在问题,但我遇到了相同的错误。
我还尝试使用db.Entry(model).State = EntityState.Modified;
进行保存,但由于我使用的是EF 4.0,因此无效。
非常感谢任何帮助!
修改
我想我知道什么是错的,模型是由一堆不同的数据库表组成的,即没有表ModContract。我很确定我必须以某种方式将模型内容分散到适当的表和列中。
编辑2
OutlookMediaEntities1的Pastebin:http://pastebin.com/YyrhjGDA
编辑3
这是GET ModifyContract方法,它显示了我如何查询数据库以制作模型。
public ActionResult ModifyContract(int id)
{
ViewData["contractid"] = id; // save id
// get data from db
OutlookMediaEntities1 db = new OutlookMediaEntities1();
var cont = from printContractAd in db.print_contract_ads
from printContracts in db.print_contracts
where printContracts.contract_id == id
select new { printContracts.contract_id, printContracts.note }; // name
// select the ads for this contract
var ads = from printContractAd in db.print_contract_ads
from printAdOption in db.print_ad_option
from printContract in db.print_contracts
from printProduct in db.print_products
from contractAdCount in db.Contract_ad_edition_count
where printContractAd.print_ad_option_id == printAdOption.print_ad_option_id &&
printContract.contract_id == printContractAd.contract_id &&
printProduct.print_product_id == printAdOption.print_product_id &&
contractAdCount.contract_ad_id == printContractAd.contract_ad_id &&
printContract.contract_id == id
select new
{
printContractAd,
printAdOption,
printProduct,
printContract.IsSigned,
contractAdCount.CountFreq
};
ModContract con = new ModContract();
foreach (var c in cont) // only one
{
con.contract_id = c.contract_id;
con.contract_name = c.note;
break; // only one
}
List<ModAds> cads = new List<ModAds>();
foreach (var ad in ads)
{
ModAds mad = new ModAds();
mad.contr_ad_id = ad.printContractAd.contract_ad_id;
mad.product_name = ad.printProduct.product_name;
mad.adv_product = ad.printAdOption.name;
mad.name = ad.printContractAd.note;
mad.freq_disc = ad.printContractAd.frequency_discount;
mad.other_dis_dol = ad.printContractAd.other_discount;
mad.other_dis_per = ad.printContractAd.other_discount_percent;
mad.non_cash_note = ad.printContractAd.non_cash_note;
mad.non_cash_cons = ad.printContractAd.non_cash_discount; // consideration?
// get editions
var eds = from contractEditions in db.print_contract_editions
where contractEditions.contract_ad_id == ad.printContractAd.contract_ad_id
select new
{
contractEditions
};
mad.editions = new List<string>();
foreach (var ed in eds)
{
mad.editions.Add("Volume " + ed.contractEditions.vol.ToString() + " Issue " + ed.contractEditions.issue.ToString());
}
cads.Add(mad);
}
con.ads = cads;
// bind dropdownlist ad options
var printproductList = from printProduct in db.print_products
select printProduct;
List<SelectListItem> products_list = new List<SelectListItem>();
List<print_products> products = printproductList.ToList();
foreach (print_products product in products)
{
SelectListItem temp_item = new SelectListItem();
temp_item.Text = product.product_name;
temp_item.Value = product.print_product_id.ToString();
products_list.Add(temp_item);
}
ViewData["products_list"] = new SelectList((IEnumerable<SelectListItem>)products_list.ToList(), "Value", "Text");
// return view with form to modify
return View(con);
}
编辑4
这也是视图,如果它有帮助(我无法在此处获得格式):http://pastebin.com/SZvzJ9FB
答案 0 :(得分:1)
为了更新实体,实体必须附加到Data Context
。为此,使用Single
方法从数据上下文获取实体就足够了。
尝试更改您的代码:
[HttpPost]
public ActionResult ModifyContract(ModContract mod)
{
// submit modified contract
if (ModelState.IsValid)
{
OutlookMediaEntities1 db = new OutlookMediaEntities1();
var updatedMod = db.ModContract.Single(m=>m.contract_id == mod.contract_id);
TryUpdateModel<ModContract>(updatedMod);
// save changes
db.SaveChanges();
return RedirectToAction("ContractDetails", "Contract", new { id = (int) ViewData["contractid"] });
}
else
{
ModelState.AddModelError("", "Missing necessary information");
return View();
}
}
答案 1 :(得分:0)
您可以在ModAds类中添加主键上方的[Key]属性。
答案 2 :(得分:0)
你可以告诉EF contract_id
是关键:
public class ModContract
{
[Key]
public int contract_id;
}
并确保属性映射到的数据库列是 PK
。根据你使用它的方式,它可能是IDENTITY
或不重要。