Skip some objects in EntitySet from DB operations (insert)
-
Thursday, March 07, 2013 7:11 PM
Hi,
I was wondering if it is possible to skip certain records in EntitySet from updating (insert/delete/update) when calling SubmitChanges.
I still want these objects being available for runtime manipulations, but be excluded from DB operations.
public class SomeWillBeSkipped { [Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true)] public long ID { get; set; } [Column(CanBeNull = false)] public long MainHolderID { get; set; } [Column(CanBeNull = false)] public string Name { get; set; } private EntityRef<PriceTableSeatType> _SeatType; [Association(Storage = "_MainHolder", IsForeignKey = true, ThisKey = "MainHolderID", OtherKey = "ID")] public MainHolder MainHolder { get { return this._MainHolder.Entity; } set { this._MainHolder.Entity = value; } } } public class MainHolder { [Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true)] public long ID { get; set; } private EntitySet<SomeWillBeSkipped> _SomeWillBeSkippeds; [Association(Storage = "_SomeWillBeSkippeds", ThisKey = "ID", OtherKey = "MainHolderID")] public EntitySet<SomeWillBeSkipped> SomeWillBeSkippeds { get { return this._SomeWillBeSkippeds; } set { this._SomeWillBeSkippeds.Assign(value); } } }So with class definition above when I do add several SomeWillBeSkipped to MainHolder object I would like all of them be available at runtime, but only some participate in DB:
using (MainHolderContext db = new MainHolderContext()) { var mainHolder = new MainHolder(); mainHolder.SomeWillSkippeds.Add(new SomeWillSkippeds() {Name = "1"}; mainHolder.SomeWillSkippeds.Add(new SomeWillSkippeds() {Name = "2"}; mainHolder.SomeWillSkippeds.Add(new SomeWillSkippeds() {Name = "3"}; mainHolder.SomeWillSkippeds.Add(new SomeWillSkippeds() {Name = "4"}; db.MainHolders.InsertOnSubmit(mainHolder); db.SubmitChanges(); }So I would like to know if I can somehow (override some virtual methods or some other way) for example slip 2nd and 3rd objects in EntityRef from DB insert?
Thanks
All Replies
-
Friday, March 08, 2013 2:47 PM
Hi Haik;
Any entity that is in the DbContext / ObjectContext will be persistent to the database on SubmitChanges and there is no automatic way to exclude entities from the process. Now what can be done is to detach those entities from the DbContext / ObjectContext before executing the SubmitChanges and then attaching them back after executing SubmitChanges.
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, April 04, 2013 9:14 AM

