我需要使用WCF服务实现EF代码优先CRUD操作并在ASP.NET MVC中使用该服务
您可以从https://drive.google.com/file/d/0B8wo1tBk-CwPQlA3N2FTOUVIVzA/edit?usp=sharing
下载代码请指导我,我已经实现了WCF服务和EF代码并使用WCF测试客户端对其进行了测试,它工作正常但是当我尝试在IIS MVC中使用它之后在IIS中使用它然后不同种类错误来了
您的参考代码如下:
服务合同
[ServiceContract]
public interface IPerson
{
[OperationContract]
List<Person> GetAllPersons();
[OperationContract]
Person GetPersonsByID(int PID);
[OperationContract]
int InsertPerson(Person oPerson);
[OperationContract]
int UpdatePerson(Person oPerson);
[OperationContract]
int DeletePerson(Person oPerson);
}
人员类作为数据合同
[DataContract(IsReference = true)]//(IsReference = true)
public class Person
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Firstname { get; set; }
[DataMember]
public string Lastname { get; set; }
}
上下文类:
public class DataContext : DbContext
{
public DataContext(): base("DataContext")
{
Configuration.AutoDetectChangesEnabled = true;
Configuration.ProxyCreationEnabled = true;
Configuration.ValidateOnSaveEnabled = true;
}
protected override void Dispose(bool disposing)
{
Configuration.LazyLoadingEnabled = false;
base.Dispose(disposing);
}
public DbSet<Person> Persons { get; set; }
}
服务实施类:
public class PersonServices : IPerson
{
public List<Person> GetAllPersons()
{
List<Person> lstp = new List<Person>();
try
{
using (var ctx = new DataContext())
{
lstp = ctx.Persons.ToList();
}
return lstp;
}
catch (Exception exp)
{
MyFaultException theFault = new MyFaultException();
theFault.Reason = "Some Error " + exp.Message.ToString();
throw new FaultException<MyFaultException>(theFault);
}
}
public Person GetPersonsByID(int PID)
{
Person oprsn = new Person();
using (var ctx = new DataContext())
{
oprsn = ctx.Persons.Find(PID);
}
return oprsn;
}
public int InsertPerson(Person oPerson)
{
int rtrnValue = 0;
using (var db = new DataContext())
{
db.Persons.Add(oPerson);
rtrnValue = db.SaveChanges();
}
return rtrnValue;
}
}
让我知道这个
我得到的错误是:1。无法创建数据库但是当WCF经过单元测试时它工作正常但是从MVC消耗时出现问题