得到错误'无法隐式转换类型' [英] getting an error 'can not implicitly convert type'

查看:76
本文介绍了得到错误'无法隐式转换类型'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的资源代码:

My repositoty code:

public MedicalGroupModel Add(MedicalGroupModel model)
       {
           Data.MedicalGroup MedicalgroupEF = new Data.MedicalGroup();

           MedicalgroupEF.MedicalGroupName = model.MedicalGroupName;
           MedicalgroupEF.AddressId = model.AddressId;
           MedicalgroupEF.ContactId = model.ContactId;

           int id = 0;
           using (Data.RxMedicaDBEntities content = new Data.RxMedicaDBEntities())
           {
               content.AddToMedicalGroups(MedicalgroupEF);
               content.SaveChanges();

               id = MedicalgroupEF.MedicalGroupId;
           }
           model = Get(id);
           return model;
       }





控制器:



controller:

public ActionResult Add([DataSourceRequest] DataSourceRequest request, MedicalGroupModel model)
       {
           List<RxMedica.Model.MedicalGroupModel> medicalgroupList = new List<MedicalGroupModel>();
           medicalgroupList = RxMedica.Configuration.ServiceLocator.MedicalgroupHandler.Add(model);

           return Json(medicalgroupList.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
       }

推荐答案

您的模型是MedicalGroupModel对象列表



Your model is a list of MedicalGroupModel objects

List<MedicalGroupModel> medicalgroupmodel = new List<MedicalGroupModel>();





但是当你添加它时你正在添加一个类型为MedicalGroup的类




but when you add to it you are adding a class of type MedicalGroup

var newgroup = new MedicalGroup();
{
    Medicalgroupname = newgroup.MedicalGroupName;
    AddressId = newgroup.AddressId;
    ContactId = newgroup.ContactId;
}





也许AddressId和ContactId等属性在MedicalGroupModel类上?即你的意思是这个





Maybe the AddressId and ContactId etc properties are on the MedicalGroupModel class instead? ie did you mean this

var newgroup = new MedicalGroupModel();
{
    Medicalgroupname = newgroup.MedicalGroupName;
    AddressId = newgroup.AddressId;
    ContactId = newgroup.ContactId;
}





至于IDisposable错误,如果你想在使用块中使用某些东西,它必须实现IDisposible ,所以如果你的RxMedicaDBEntities没有那么修改就行了。





As for the IDisposable error, if you want to use something in a "using" block it must implement IDisposible, so if your RxMedicaDBEntities doesn't then amend it so it does.

public class RxMedicaDBEntities : IDisposable


如果您获得在EF DbContext中使用错误,通常是因为你没有将EntityFramework添加到使用上下文的项目中。
If you're getting the using error with a EF DbContext, its usually because you didn't add the EntityFramework to the project that's using the context.


这篇关于得到错误'无法隐式转换类型'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆