asp.net mvc 中的这个插入方法有什么问题?
我的控制器在插入时调用存储库类方法,
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")]FormCollection collection)
{
try
{
MaterialsObj materialsObj = new MaterialsObj();
materialsObj.Mat_Name = collection["Mat_Name"];
materialsObj.Mes_Id = Convert.ToInt64(collection["MeasurementType"]);
materialsObj.Mes_Name = collection["Mat_Type"];
materialsObj.CreatedDate = System.DateTime.Now;
materialsObj.CreatedBy = Convert.ToInt64(1);
materialsObj.IsDeleted = Convert.ToInt64(1);
consRepository.createMaterials(materialsObj);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
并且我的存储库类具有此方法,
public MaterialsObj createMaterials(MaterialsObj materialsObj)
{
db.Materials.InsertOnSubmit(materialsObj);
return materialsObj;
}
但是当我编译此方法时,我得到 The best 重载方法匹配 'System.Data.Linq.Table
... 无法从“CrMVC.BusinessObjects.MaterialsObj”转换为“CrMVC.Models.Material”
..
我错过了什么吗?
My controller calls a repository class method on insert,
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")]FormCollection collection)
{
try
{
MaterialsObj materialsObj = new MaterialsObj();
materialsObj.Mat_Name = collection["Mat_Name"];
materialsObj.Mes_Id = Convert.ToInt64(collection["MeasurementType"]);
materialsObj.Mes_Name = collection["Mat_Type"];
materialsObj.CreatedDate = System.DateTime.Now;
materialsObj.CreatedBy = Convert.ToInt64(1);
materialsObj.IsDeleted = Convert.ToInt64(1);
consRepository.createMaterials(materialsObj);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
and my repository class has this,
public MaterialsObj createMaterials(MaterialsObj materialsObj)
{
db.Materials.InsertOnSubmit(materialsObj);
return materialsObj;
}
But when i compile this i get The best overloaded method match for 'System.Data.Linq.Table<CrMVC.Models.Material>.InsertOnSubmit(CrMVC.Models.Material)' has some invalid arguments
...cannot convert from 'CrMVC.BusinessObjects.MaterialsObj' to 'CrMVC.Models.Material'
..
am i missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您传递给
InsertOnSubmit
的对象必须是 LINQ-to-SQL 类之一。在本例中,您尝试插入 LINQ-to-SQL 不知道的类型的对象(您的业务对象之一,而不是 LINQ-to-SQL 类型)。
有几种方法可以克服这个问题。一次是将业务对象转换为存储库中适当的 LINQ-to-SQL 类。另一种方法是在两者之间创建隐式转换,然后让 .NET 处理其余部分。
存储库转换代码
隐式转换代码(添加到业务对象类的方法)
The object you pass to
InsertOnSubmit
has to be one of your LINQ-to-SQL Classes.In this case, you're trying to insert an Object of a type that LINQ-to-SQL has no idea about (one of your business objects, not a LINQ-to-SQL type).
There are several ways to overcome this. Once is to convert the business object to the appropriate LINQ-to-SQL class in your Repository. The other is to create an implicit cast between the two and let .NET handle the rest.
Repository Cast Code
Implicit Cast Code (method added to the Business Object class)
您可能需要添加 db.SubmitChanges() 来提交更改
you might want to add db.SubmitChanges() to commit the changes