0

Suppose I have few records which i want to insert/update through EF.

public partial class Contact
{
        public int ContactID { get; set; }
        public string ContactPerson { get; set; }
        public string EmailID { get; set; }
}

var _Contact = new list<Contact>();
_Contact.add(new Contact(){ContactID=1,ContactPerson="Dev",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=2,ContactPerson="Ross",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=3,ContactPerson="Martin",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=4,ContactPerson="Moss",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=5,ContactPerson="Koos",EmailID="[email protected]"});

With the help of a stored procedure, we can detect that any particular records exist or not, if exist then update will execute and if not exist then records will be inserted. i want to do the same through EF.......is it possible ?

I know that I can call SQL Server stored procedures from EF to achieve my goal but I am trying to know if I can do it in EF without calling a stored procedure?

If yes then discuss the same with sample code which check each records exist in db or not and if exist then it will update otherwise will insert. Looking for help.

2

1 Answer 1

1

Since EF 4.3 there is an AddOrUpdate ExtensionMethod for DbSet. in Namespace System.Data.Entity.Migrations

public partial class Contact
{
    public int ContactID { get; set; }
    public string ContactPerson { get; set; }
    public string EmailID { get; set; }
}

var _Contact=new list<Contact>();
_Contact.add(new Contact(){ContactID=1,ContactPerson="Dev",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=2,ContactPerson="Ross",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=3,ContactPerson="Martin",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=4,ContactPerson="Moss",EmailID="[email protected]"});
_Contact.add(new Contact(){ContactID=5,ContactPerson="Koos",EmailID="[email protected]"});

// check by Primary key
context.Contacts.AddOrUpdate(_Contacts)
// or check by EmailID
context.Contacts.AddOrUpdate(p => p.EmailID, _Contacts)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your answer but if i need to see what SQL AddOrUpdate() will generate then how do i know? suppose without using SQL server profiler how can i hook AddOrUpdate() function in code which write or dupm the sql to console generate by AddOrUpdate() function....any idea??
is there any site which show the feature added in EF version wise. suppose AddOrUpdate() added in EF version 4.3. so what other feature has been introduce in prev or next version in EF. i want site which show or discuss EF related feature version wise with example code & details. please guide if u have any idea. thanks
plzz see this code. / check by Primary key context.Contacts.AddOrUpdate(_Contacts) u said it would check by PK but if there is more than one PK i mean composite PK is there...the how it will handle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.