Adding data to my database using entity framework in winform

c# entity-framework-6 winforms

Question

I'm new to using Windows forms. I'm trying add data to my database using entity frame work 6. I have created my entity model, and a data datasource as what I have read that I need to do because I can't reference my entity model without it.

So far, I have a button that I want to use to add the data. I'm not going to use a grid or something like that. I'm just trying to add data I assigned manually.

I wanted to do IMF_Main.SaveChanges(); or something similar.

Here is my code so far:

public partial class frm_Main : Form
    {       
        C_TEST_IMF_Main IMF_Main;

        public frm_Main()
        {
            InitializeComponent();
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            IMF_Main = new C_TEST_IMF_Main();
            IMF_Main.BrandID = "TAE1";
            IMF_Main.CategoryID = "TAE2";
            IMF_Main.SubCategoryID = "TAE3";
            IMF_Main.ClassID = "TAE4";
            IMF_Main.GenderID = "TAE5";
            IMF_Main.First_SRP = Convert.ToDecimal("1.00");
            IMF_Main.Current_SRP = Convert.ToDecimal("2.00");
            IMF_Main.Previous_SRP = Convert.ToDecimal("3.00");
            IMF_Main.isActive = Convert.ToBoolean(1);
            IMF_Main.DateCreated = DateTime.Now;
            IMF_Main.CoaID = "TAETAETAETAE";
            IMF_Main.SubCoaID = "TAETAETAETAE2";

        }
    }

And here is my structure: Additional Note: GroundCommander is the name of my Database and GroundCommanderEntities is my entity model

enter image description here

1
0
7/1/2015 4:57:17 AM

Accepted Answer

You need to add an object of IMF_Main and than call SaveChanges of context class. At the end of your code write below code.

using(POS_ODS obj=new POS_ODS())
{
 obj.IMF_Main.Add(IMF_Main);
 obj.SaveChanges();
}

Here POS_ODS is name of your DBContext class.

1
7/1/2015 4:03:52 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow