我正在尝试将图像添加到数据库中的表中,但在使用 WCF 服务时似乎无法执行此操作
下面的代码给出了一个错误
private CRMEntities _crm;
private readonly Uri _uri = new Uri("http://localhost/CRMService.svc");
//Adds a new image to database
public bool AddImage(byte[] imagefile, int epid)
{
if (imagefile.Equals(null) || epid.Equals(null)) return false;
_crm = new CRMEntities(_uri);
var i = new Image { ImageFile = imagefile, EP_ID = epid };
_crm.AddToImages(i);
_crm.SaveChanges();
return true;
}
截屏:
但是如果我把它改成这个,它就可以保存得很好
if (imagefile.Equals(null) || epid.Equals(null)) return false;
var crm = new CRMData.CRMEntities(System.Configuration.ConfigurationManager.ConnectionStrings["CRMEntities"].ToString());
var i = new CRMData.Image { ImageFile = imagefile, EP_ID = epid };
crm.AddToImages(i);
crm.SaveChanges();
return true;
编辑
但它适用于其他类。
像
private CRMEntities _crm;
private readonly Uri _uri = new Uri("http://localhost:1677/CRMService.svc");
//METHODS
//SAVE
public bool AddEmailProblem(string description, DateTime datecreated, int clientid, string mailId)
{
if (description == null || clientid == 0 || mailId == null) return false;
_crm = new CRMEntities(_uri);
var objep = new EmailProblem
{
Description = description,
DateCreated = datecreated,
CLIENT_ID = clientid,
Mail_ID = mailId
};
_crm.AddToEmailProblems(objep);
_crm.SaveChanges();
return true;
}
保存到数据库。
我正在使用的连接字符串是 -
<connectionStrings>
<add name="CRMEntities" connectionString="metadata=res://*/CRM.csdl|res://*/CRM.ssdl|res://*/CRM.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLExpress;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=Server01;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
<add name="CRM" connectionString="Data Source= .\SQLExpress;Initial Catalog=CRM;Persist Security Info=True;User ID=sa;Password=Server01" providerName="System.Data.SqlClient" />