如何更改保存在数据库中的查询?

时间:2019-02-24 19:56:21

标签: c# entity-framework entity-framework-6

我正在使用:

  • 实体框架v6.2.0
  • SQL Server 2012

我有这些表:
-tbl_979F301_Groups
-tbl_979F302_学生
-tbl_979F303_GroupsStud(FK-tbl_979F301_Groups)

请求

  • vw_979F3_GroupsStud_Stud;
    enter image description here

场景:

  • 用户在客户端SQL Server 2012中
  • 用户更改查询vw_979F3_GroupsStud_Stud;
  • 更改显示在查询中使用的表中

enter image description here

enter image description here

如果我通过Entity Framework对使用表的应用程序进行了更改,则会收到错误消息:

  

无法更新EntitySet集vw_979F3_GroupsStud_Stud,因为   它具有DefiningQuery请求,并且缺少<UpdateFunction>元素   在<ModificationFunctionMapping>元素中以支持当前   操作

enter image description here

问题:如何在查询中将对应用程序所做的更改保存到数据库中?

表结构:

CREATE TABLE "tbl_979F301_Groups" 
(
    "id_group" BIGINT NOT NULL,
    "nameGroup" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_1_Group" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_2_Group" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_3_Group" NVARCHAR(255) NULL DEFAULT NULL,
    PRIMARY KEY ("id_group")
)

CREATE TABLE "tbl_979F302_Students"  
(
    "id_stud" BIGINT NOT NULL,
    "NameStud" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_1" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_2" NVARCHAR(255) NULL DEFAULT NULL,
    "Property_3" NVARCHAR(255) NULL DEFAULT NULL,
    PRIMARY KEY ("id_stud")
)

CREATE TABLE "tbl_979F303_GroupsStud" 
(
    "id_groupStud" BIGINT NOT NULL,
    "id_group" BIGINT NULL DEFAULT NULL,
    "id_stud" BIGINT NULL DEFAULT NULL,
    "groupStud_descript" NVARCHAR(255) NULL DEFAULT NULL,
    PRIMARY KEY ("id_groupStud")
); 

查看vw_979F3_GroupsStud_Stud

SELECT tbl_979F303_GroupsStud.*, tbl_979F302_Students.NameStud
FROM tbl_979F303_GroupsStud
INNER JOIN tbl_979F302_Students ON tbl_979F303_GroupsStud.id_stud = tbl_979F302_Students.id_stud;

我的代码。

 ContextDBF3 cntDBF3;
 int id_group_cur;

 public Frm3UC()
 {
     InitializeComponent();

     cntDBF3 = new ContextDBF3();
 }

 private void Frm3UC_Load(object sender, EventArgs e)
 {
     FillGrid_1();
 }

 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
 {
     DataGridViewRow selectedRow = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];

     textBox1.Text = selectedRow.Cells[0].Value.ToString();
     id_group_cur = Convert.ToInt32(selectedRow.Cells[0].Value);

     FillGrid_2();
 }

 public void FillGrid_1()
 {
     try
     {
         cntDBF3.tbl_979F301_Groups.Load();

         bs_Grid_1.DataSource = cntDBF3.tbl_979F301_Groups.Local.ToBindingList();

         dataGridView1.DataSource = bs_Grid_1;
     }
     catch (Exception ex)
     {
         string s = ex.Message;
         string t = ex.StackTrace;
         // throw;
         MessageBox.Show(s);
     }
 }

 public void FillGrid_2()
 {
     try
     {
         IQueryable<vw_979F3_GroupsStud_Stud> query = cntDBF3.vw_979F3_GroupsStud_Stud
               // .Select(x => x)
                                                             .Where(x => x.id_group == id_group_cur);

         bs_Grid_2.DataSource = query.ToList();
         dataGridView2.DataSource = bs_Grid_2;
     }
     catch (Exception ex)
     {
         string s = ex.Message;
         string t = ex.StackTrace;
         // throw;
         MessageBox.Show(s);
     }
 }

 public void SaveContext()
 {
     try
     {
         cntDBF3.SaveChanges();
     }
     catch (Exception ex)
     {
         string s = ex.Message;
         string t = ex.StackTrace;
         // throw;
         MessageBox.Show(s);
     }
 }

0 个答案:

没有答案