mercredi 29 juin 2016

EF MVC5 Save in different databases

I want to be able to commit new entries in my application's resident database and a similar table in another database with different connection string. I am using Identity2 and Mysql database

I have added the connection string to this new database in web.config But I am not sure how to call and add to other database. Below is what I have tried. Any help will be appreciated

 public class MemberController : Controller
    {
        private DB1 db = new DB1();
        private Db2 db1 = new Db2();

          [HttpPost]
            public ActionResult Create(Member member)
            {
                if (ModelState.IsValid)
                {
                    db1.Customers.Add(member);
                    db.Members.Add(member);
                    int year = Convert.ToInt32(Request["DOB"]);
                    int month = Convert.ToInt32(Request["MOB"]);
                    int day = Convert.ToInt32(Request["BDay"]);
                     if(year>0&&month>0&&day>0){
                         DateTime dateb = new DateTime(year, month, day);

                         member.DateOBirth = dateb;
                     }
                    member.AddDate = DateTime.Now;
                    member.UpDated = DateTime.Now;
                    db.SaveChanges();
                    db2.SaveChanges();


                return RedirectToAction("Index");
            }
}

Error I get is Error 22 'Your.Models.CoDb' does not contain a definition for 'Customers' and no extension method 'Customers' accepting a first argument of type 'Your.Models.CoDb' could be found (are you missing a using directive or an assembly reference?)

Aucun commentaire:

Enregistrer un commentaire