使用DTO实体框架,如何更新数据库?

时间:2013-09-13 10:06:53

标签: c# .net entity-framework

我有以下代码,可以让我完全按照自己的意愿显示gridview,

我需要将2个表单复选框移动到同一个单元格,但可以在之后完成。

我正在尝试根据DGV中的编辑更新我的数据库,我已将Name,DOB / DOD,死亡之地,Crem / Burial,葬礼日期和COR设置为只读,这些值不需要更新回来。

有没有办法将DTO映射回实体框架或更好的方式?

由于

 private void RefreshDataGrid()
    {
        var query = (from f in context.funerals
                     where f.IsPencil == 0
                     join d in context.deceaseddetails on f.DeceasedID equals d.ID
                     join i in context.funeralservices on f.ID equals i.FuneralID
                     where i.IsAlternative == 0
                     join h in context.htvalues on f.HtValuesID equals h.ID
                     join p in context.placeofdeaths on f.PlaceOfDeathID equals p.ID
                     join c in context.coroners on f.CoronerID equals c.ID
                     let val1 = d.DateOfDeath
                     let val2 = d.DateOfBirth
                     let val3 = i.Date
                     orderby i.Date
                     select new
                     {
                         d.LastName,
                         d.FirstName,
                         val2,
                         val1,
                         f.CremOrInt,
                         FormsSigned1 = h.FormsSigned1,
                         FormsSigned2 = h.FormsSigned2,
                         PlaceOfDeath = p.PlaceOfDeath1.Substring(0, 15),
                         val3,
                         HospOrDocInformed = h.HospOrDocInformed ?? "Unspecified",
                         CHQorCoroner = h.CHQorCoroner ?? "Unspecified",
                         CertReq = h.CertReq ?? "Unspecified",
                         RestingAt = h.RestingAt ?? "Unspecified",
                         CoffinModel = h.CoffinModel ?? "Unspecified",
                         SizeAct = h.CoffinSize ?? "Unspecified",
                         CoffinPlateComp = h.CoffinPlateComp,
                         HtBy = h.HtBy ?? "Unspecified",
                         COR = c.Abbrieviation ?? "Unspecified",
                         Information = h.HTInformation ?? "Unspecified",
                         RedStatus = h.RedStatus,
                         YellowStatus = h.YellowStatus,
                         GreenStatus = h.GreenStatus
                     }).ToList();

        var dataobjects = query.Select(d => new DataBindingProjection
        {
            DeceasedName = (d.FirstName + Environment.NewLine + d.LastName),
            DOBDOD = (d.val2.HasValue ? d.val2.Value.ToShortDateString() : string.Empty) + Environment.NewLine +
            (d.val1.HasValue ? d.val1.Value.ToShortDateString() : string.Empty),
            DeathPlace = d.PlaceOfDeath,
            CremInt = d.CremOrInt,
            FuneralDate = (d.val3.HasValue ? d.val3.Value.ToShortDateString() : string.Empty),
            HospDoc = d.HospOrDocInformed,
            CHQCor = d.CHQorCoroner,
            forms1 = d.FormsSigned1,
            forms2 = d.FormsSigned2,
            CertReq = d.CertReq,
            RestingAt = d.RestingAt,
            CoffinModel = d.CoffinModel,
            SizeAct = d.SizeAct,
            CoffinPlate = d.CoffinPlateComp,
            HtBy = d.HtBy,
            COR = d.COR,
            Information = d.Information,
            red = d.RedStatus,
            yellow = d.YellowStatus,
            green = d.GreenStatus

        }).ToList();

        dataGridView1.DataSource = dataobjects;

        dataGridView1.Columns[0].HeaderText = "Last Name" + Environment.NewLine + "First Name";
        dataGridView1.Columns[0].ReadOnly = true;
        dataGridView1.Columns[1].HeaderText = "DOB" + Environment.NewLine + "DOD";
        dataGridView1.Columns[1].ReadOnly = true;
        dataGridView1.Columns[2].HeaderText = "Place Of" + Environment.NewLine + "Death";
        dataGridView1.Columns[2].ReadOnly = true;
        dataGridView1.Columns[3].HeaderText = "Crem/Int";
        dataGridView1.Columns[3].ReadOnly = true;
        dataGridView1.Columns[4].HeaderText = "Funeral" + Environment.NewLine + "Date";
        dataGridView1.Columns[4].ReadOnly = true;
        dataGridView1.Columns[5].HeaderText = "Hosp or" + Environment.NewLine + "Doc Informed";
        dataGridView1.Columns[6].HeaderText = "CHQ or" + Environment.NewLine + "Coroner";
        dataGridView1.Columns[7].HeaderText = "Forms 1" + Environment.NewLine + "Signed?";
        dataGridView1.Columns[8].HeaderText = "Forms 2" + Environment.NewLine + "Signed?";
        dataGridView1.Columns[9].HeaderText = "Cert" + Environment.NewLine + "Req.";
        dataGridView1.Columns[10].HeaderText = "Resting" + Environment.NewLine + "@";
        dataGridView1.Columns[11].HeaderText = "Coffin" + Environment.NewLine + "Model";
        dataGridView1.Columns[12].HeaderText = "Size" + Environment.NewLine + "Act.";
        dataGridView1.Columns[13].HeaderText = "Coffin" + Environment.NewLine + "Plate Comp";
        dataGridView1.Columns[14].HeaderText = "HT" + Environment.NewLine + "By";
        dataGridView1.Columns[15].HeaderText = "COR";
        dataGridView1.Columns[15].ReadOnly = true;
        dataGridView1.Columns[16].HeaderText = "Information" + Environment.NewLine + "Of Interest";
        dataGridView1.Columns[17].HeaderText = "Red" + Environment.NewLine + "Status";
        dataGridView1.Columns[18].HeaderText = "Yellow" + Environment.NewLine + "Status";
        dataGridView1.Columns[19].HeaderText = "Green" + Environment.NewLine + "Status";
    }


    private class DataBindingProjection
    {
        public string DeceasedName { get; set; }
        public string DOBDOD {get; set;}
        public string DeathPlace { get; set; }
        public string CremInt { get; set; }
        public string FuneralDate { get; set; }
        public string HospDoc { get; set; }
        public string CHQCor { get; set; }
        public Boolean forms1 { get; set; }
        public Boolean forms2 { get; set; }
        public string CertReq { get; set; }
        public string RestingAt { get; set; }
        public string CoffinModel { get; set; }
        public string SizeAct { get; set; }
        public Boolean CoffinPlate { get; set; }
        public string HtBy { get; set; }
        public string COR { get; set; }
        public string Information { get; set; }
        public Boolean red { get; set; }
        public Boolean yellow { get; set; }
        public Boolean green { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

Nilesh的评论工作得很好,谢谢!

使用数据传输对象,然后将DTO绑定到EF!