How to make changes to the query saved in the database?

杀马特。学长 韩版系。学妹 提交于 2019-12-13 04:17:24

问题


I'm using:

  • Entity Framework v6.2.0
  • SQL Server 2012

I have these tables:  
- tbl_979F301_Groups
- tbl_979F302_Students
- tbl_979F303_GroupsStud (FK - tbl_979F301_Groups)

  Request:

  • vw_979F3_GroupsStud_Stud;  

Scenario:

  • The user is in the client SQL Server 2012
  • The user makes changes to the query vw_979F3_GroupsStud_Stud;
  • Changes are displayed in the tables that are used in the query

 

If I make changes to an application that uses tables through Entity Framework, then I get an error:

Failed to update EntitySet set vw_979F3_GroupsStud_Stud because it has a DefiningQuery request and is missing an <UpdateFunction> element in the <ModificationFunctionMapping> element to support the current operations

 

Question: how to save changes to the database that are made to the application in the query?

Table structure:

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")
); 

View 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;

My code.

 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);
     }
 }

来源:https://stackoverflow.com/questions/54855911/how-to-make-changes-to-the-query-saved-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!