asp.net mvc 中的这个插入方法有什么问题?

发布于 2024-08-31 06:47:15 字数 1344 浏览 5 评论 0原文

我的控制器在插入时调用存储库类方法,

 [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.InsertOnSubmit (CrMVC.Models.Material)' 有一些无效参数... 无法从“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So尛奶瓶 2024-09-07 06:47:15

您传递给 InsertOnSubmit 的对象必须是 LINQ-to-SQL 类之一。

在本例中,您尝试插入 LINQ-to-SQL 不知道的类型的对象(您的业务对象之一,而不是 LINQ-to-SQL 类型)。

有几种方法可以克服这个问题。一次是将业务对象转换为存储库中适当的 LINQ-to-SQL 类。另一种方法是在两者之间创建隐式转换,然后让 .NET 处理其余部分。

存储库转换代码

public MaterialsObj createMaterials(MaterialsObj materialsObj)
{
    CrMVC.Models.Material mat = new CrMVC.Models.Material();

    // copy properties to the Materials object from materialsObj
    db.Materials.InsertOnSubmit(mat);

    materialsObject.IdField = mat.IdField;

    return materialsObj;
}

隐式转换代码(添加到业务对象类的方法)

public static implicit operator CrMVC.Models.Material
    (CrMVC.BusinessObjects.MaterialsObj)
{
    // Add conversion code here
}

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

public MaterialsObj createMaterials(MaterialsObj materialsObj)
{
    CrMVC.Models.Material mat = new CrMVC.Models.Material();

    // copy properties to the Materials object from materialsObj
    db.Materials.InsertOnSubmit(mat);

    materialsObject.IdField = mat.IdField;

    return materialsObj;
}

Implicit Cast Code (method added to the Business Object class)

public static implicit operator CrMVC.Models.Material
    (CrMVC.BusinessObjects.MaterialsObj)
{
    // Add conversion code here
}
倚栏听风 2024-09-07 06:47:15

您可能需要添加 db.SubmitChanges() 来提交更改

you might want to add db.SubmitChanges() to commit the changes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文