What am i missing? Tables in my DataContext do not have Add or Remove options
-
Pretty much what it says in the title, i must be missing something obvious. My "tables" inside my datacontext do not have Remove or Add functions, for example i would like to do something like...
MyDataContext db = new MyDataContext()
db.Clients.Add(myCleint);
db.SubmitChanges();but the Add function is not available to me in the db.Clients object. Anyone able to shed some light on this?
-
Pretty much what it says in the title, i must be missing something obvious. My "tables" inside my datacontext do not have Remove or Add functions, for example i would like to do something like...
MyDataContext db = new MyDataContext()
db.Clients.Add(myCleint);
db.SubmitChanges();but the Add function is not available to me in the db.Clients object. Anyone able to shed some light on this?
Add is actually called
InsertOnSubmit
, and Remove isDelete
.Deja View - the feeling that you've seen this post before.
-
Add is actually called
InsertOnSubmit
, and Remove isDelete
.Deja View - the feeling that you've seen this post before.
Assalam-0-alaikum, Well for that I think u need to know how linqtosql works. then u could have a better idea how to add, update and delete tables in datacontext class. As Soon as u create a linqtosql class(.dbml) its creates a datacontext class and also creates classes for the individual tables that you add in the .dbml class later on. now to insert, you are required to get table from linqtosql class that u have created using following statement. yourDataContextClass datacontext = new yourDataContextClass() ; Table obj = datacontext.getTable(); Now create object on class that has been created for your table as follows. yourTableclass objYourClass = new yourTableclass(); Now add values for the fields. then, obj.InsertOnSubmit(objYourClass); datacontext.submitchanges(); Hope it gives u a bit of idea. Regards, Atif Ali Bhatti.
-
Assalam-0-alaikum, Well for that I think u need to know how linqtosql works. then u could have a better idea how to add, update and delete tables in datacontext class. As Soon as u create a linqtosql class(.dbml) its creates a datacontext class and also creates classes for the individual tables that you add in the .dbml class later on. now to insert, you are required to get table from linqtosql class that u have created using following statement. yourDataContextClass datacontext = new yourDataContextClass() ; Table obj = datacontext.getTable(); Now create object on class that has been created for your table as follows. yourTableclass objYourClass = new yourTableclass(); Now add values for the fields. then, obj.InsertOnSubmit(objYourClass); datacontext.submitchanges(); Hope it gives u a bit of idea. Regards, Atif Ali Bhatti.
sorry forgot to mention that u can udpate and delete in the above mentioned manner too. Regards. Atif Ali Bhatti.
-
Assalam-0-alaikum, Well for that I think u need to know how linqtosql works. then u could have a better idea how to add, update and delete tables in datacontext class. As Soon as u create a linqtosql class(.dbml) its creates a datacontext class and also creates classes for the individual tables that you add in the .dbml class later on. now to insert, you are required to get table from linqtosql class that u have created using following statement. yourDataContextClass datacontext = new yourDataContextClass() ; Table obj = datacontext.getTable(); Now create object on class that has been created for your table as follows. yourTableclass objYourClass = new yourTableclass(); Now add values for the fields. then, obj.InsertOnSubmit(objYourClass); datacontext.submitchanges(); Hope it gives u a bit of idea. Regards, Atif Ali Bhatti.
I think you meant to post this as a reply to the OP ;) ; I already know this.
Deja View - the feeling that you've seen this post before.
-
I think you meant to post this as a reply to the OP ;) ; I already know this.
Deja View - the feeling that you've seen this post before.
yeah definitely.
-
yeah definitely.
Atif Ali Bhatti wrote:
yeah definitely.
:-D
Deja View - the feeling that you've seen this post before.
-
Atif Ali Bhatti wrote:
yeah definitely.
:-D
Deja View - the feeling that you've seen this post before.
Hi Guys, i had the same issue after reading your post i figured it out! Thanks. I thought i would pop some code on it for all to see :-\ hope it helps someone like u guys helped me public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { AddUser(); BindData(); } public UserClass AddUser() { DataClasses1DataContext db = new DataClasses1DataContext(); //Where User is the Class Generated by LINQ that is mapped to my DB Table LinqMapping.User user = new User(); user.Name = "Butch"; user.Surname = "Zn"; user.Active = true; db.Users.InsertOnSubmit(user); db.SubmitChanges(); return null; } public ICollection<UserClass> GetUsers() { DataClasses1DataContext db = new DataClasses1DataContext(); var userList = from u in db.Users select u; ICollection<UserClass> list = new List<UserClass>(); foreach (var user in userList) { list.Add(new UserClass {Id = user.id, FirstName = user.Name, Surname = user.Surname, Active = user.Active}); } return list; } public void BindData() { ICollection<UserClass> user = GetUsers(); GridView1.DataSource = user; GridView1.DataBind(); }