实体框架SaveChanges()无效

时间:2015-09-09 12:07:48

标签: c# entity-framework wcf xml-serialization

我正在练习wcf和实体框架。在我的情况下,我有两个wcf服务,一个用于读取操作,另一个用于crud操作。在我的前端,我使用C#表单。我从wcf服务引用中获取了一条记录,用于读取,进行了一些修改并通过wcf crud服务将其保存到数据库中。我从操作中收到一个真实的响应,表明它不会抛出任何错误,但更改不会保存在db中。我正在使用冒险作品进行测试。

CRUD wcf:

public class AWCRUD : IAWCRUD
{
    public bool updatePerson(Person person)
    {
        try
        {
            using (AWEntities ctx = new AWEntities())
            {
                ctx.People.Attach(person);
                ctx.Entry(person).State = EntityState.Modified;
                ctx.SaveChanges();
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public bool updateEmployee(Employee employee)
    {
        try
        {
            using (AWEntities ctx = new AWEntities())
            {
                ctx.Employees.Attach(employee);
                ctx.Entry(employee).State = EntityState.Modified;
                ctx.SaveChanges();
                return true;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

}

阅读wcf:

public class WCFAWRead : IWCFAWRead
{
    ILog log = LogManager.GetLogger(typeof(WCFAWRead));

    public PersonDetails getDetails(int entityID)
    {
        try
        {
            using (AWEntities ctx = new AWEntities())
            {
                var person = (from p in ctx.People
                              join det in ctx.Employees on p.BusinessEntityID equals det.BusinessEntityID
                              where p.BusinessEntityID == entityID
                              select new PersonDetails
                              {
                                  EntityID = p.BusinessEntityID,
                                  Name = p.FirstName,
                                  LastName = p.LastName,
                                  Title = det.JobTitle,
                                  BirthDay = det.BirthDate,
                                  Gender = det.Gender
                              }).FirstOrDefault();
                return person;
            }
        }
        catch (Exception ex)
        {
            //log.Error(ex.Message);
            return null;
        }
    }

    public List<FullPersonInfo> searchPerson(string chriteria)
    {
        try
        {
            using (AWEntities ctx = new AWEntities())
            {
                var lChriteria = chriteria.ToLower();
                var person = (from p in ctx.People
                              join det in ctx.Employees on p.BusinessEntityID equals det.BusinessEntityID
                              where p.FirstName.ToLower().Contains(lChriteria) || p.LastName.ToLower().Contains(lChriteria)
                              select new FullPersonInfo
                              {
                                  Person = p,
                                  Employee = det
                              }).ToList();
                return person;
            }
        }
        catch (Exception ex)
        {
            //log.Error(ex.Message);
            return null;
        }
    }
}

要转换从wcf服务引用创建的不同类型的Person对象,我使用以下代码:

DataContractSerializer seri = new DataContractSerializer(typeof(AWReadReference.Person));
            using (var writer = XmlWriter.Create("p.xml"))
            {
                seri.WriteObject(writer,person);
            }
            var deser = new DataContractSerializer(typeof(AWCrudReference.Person));
            using (var reader = XmlReader.Create("p.xml"))
            {
                this.person = (AWCrudReference.Person)deser.ReadObject(reader);
            }

有没有人知道它为什么会发生和解决方案?

0 个答案:

没有答案