none
Insert data to Database by using Model edmx RRS feed

  • Question

  • I create Database in visual studio as service-based Database after I make tables i tried to connect it to Model .edmx by choose update Model from Database now when I insert data to Model .edmx , it didn't insert it to Database how to make it insert data to Database by using Model and Thanks I am using Visual Studio 2010

      private  CoffeeShopDatabaseEntities1 cse = new CoffeeShopDatabaseEntities1();
        private Byte[] bytedata;
        public addproduct()
        {
            InitializeComponent();
    
            cbocatgrey.DataSource = cse.TblProductType.ToList();
            cbocatgrey.DisplayMember = "Descrption";
            cbocatgrey.ValueMember = "ProductType";
        }
    
        private void upload_image_Click(object sender, EventArgs e)
        {
            DialogResult dlg = openFileDialog1.ShowDialog();
            if (dlg == DialogResult.OK)
            {
                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                bytedata = new Byte[fs.Length];
                fs.Read(bytedata, 0, bytedata.Length);
                fs.Close();
                MemoryStream ms = new MemoryStream(bytedata);
                pload.Image = Image.FromStream(ms);
    
            }
        }
    
        private void save_Click(object sender, EventArgs e)
        {
            TblProduct product = new TblProduct();
            product.Descrption = descrptiontxt.Text;
            product.Price = decimal.Parse(pricetxt.Text);
            product.Image = bytedata;
            product.ProductType = (int)cbocatgrey.SelectedValue;
            cse.AddtoTBlProduct(product);
            cse.saveChanges();
    
    
        }


    • Edited by ai_n Thursday, October 5, 2017 9:43 AM
    Wednesday, October 4, 2017 6:59 AM

All replies

  • Show your source code that trying to insert the data.
    Wednesday, October 4, 2017 7:39 PM
  • i put the code but i don't think the problem is from the code 

    is this way right cuase i do this to connect database to model

    connect the database to Model .edmx by choose update Model in model edmx


    • Edited by ai_n Thursday, October 5, 2017 9:47 AM
    Thursday, October 5, 2017 9:46 AM
  • i put the code but i don't think the problem is from the code 

    is this way right cuase i do this to connect database to model

    connect the database to Model .edmx by choose update Model in model edmx


    yes the problem is with your code, and you need to understand that the add of the entity to EF is an entity in a disconnected state. EF doesn't know what the state of the entity is to know what to do to persist the object to the database. So EF just ignores persisting the object to the DB.

    https://msdn.microsoft.com/en-us/library/jj592676%28v=vs.113%29.aspx?f=255&MSPPError=-2147217396

    http://www.entityframeworktutorial.net/EntityFramework5/attach-disconnected-entity-graph.aspx

     
    Thursday, October 5, 2017 1:04 PM