简体   繁体   中英

Create a Customer from a Guest - using MVC3, C#, Entity Framework, Razor Views, SQL Server

I am adding a function into my program that allows Employees to convert event guest to customers, so they don't have to retype everything.

The one problem I am running into is... The guest has a phone # and rmail in the same table as the rest of their information. Customers have their phone #'s and emails stored in separate one-to-many table.

So during the conversion I need a CustomerID to assign to the email & phone number to store it to the tables.

How do I create the customer and retain the CustomerID in order to use to assign to the CustomerEmail and CustomerPhone tables?

I have a ViewModel to handle all the variables.

What do I have to do to the code below to have it assign a CustomerID in order to successfully call SaveChanges() for CustomerPhones && CustomerEmails ?

var customerNew = new Customer
                {
                    CustomerTypeId = customer.CustomerTypeId,
                    FirstName = customer.FirstName,
                    LastName = customer.LastName,
                    FullName = customer.FirstName + " " + customer.LastName,
                    Birthday = customer.Birthday,
                    GenderId = customer.GenderId,
                    CompanyName = customer.CompanyName,
                    UserName = customer.UserName,
                    FavMusicId = customer.FavMusicId,
                    OwnerId = customer.OwnerId
                };

                var phoneNew = new CustomerPhone
                {
                    CustomerId = customer.CustomerId,
                    Number = customer.Number,
                    PhoneTypeId = 5
                };

                var emailNew = new CustomerEmail
                {
                    CustomerId = customer.CustomerId,
                    Email = customer.Email
                };

                db.Customers.AddObject(customerNew);
                db.CustomerPhones.AddObject(phoneNew);
                db.CustomerEmails.AddObject(emailNew);

                db.SaveChanges();

                return RedirectToAction("Details", new { id = customer.CustomerId });

Thank you,

Tim

EXTRA Bonus Points: If you can tell me how to automatically delete the guest from the database after the customer creation, without having to click anything else. :)

If you have set up proper foreign key relationships on the database level, and your EF model has picked them up, you should be able to write:

var customerNew = new Customer
                  {
                      // ... set all the properties here
                  }

var phoneNew = new CustomerPhone
            {
                Number = customer.Number,
                PhoneTypeId = 5
            };
customerNew.CustomerPhones.Add(phoneNew);  // add new phone to the association

var emailNew = new CustomerEmail
            {
                Email = customer.Email
            };
customerNew.CustomerEMails.Add(phoneNew);  // add new e-mail to the association

db.Customers.AddObject(customerNew);
db.SaveChanges();

and let EF handle all the rest like figuring out the ID's and everything.

Sned the guest information (complete) into a stored procedure that creates the new records (customer, phone and email).

EXTRA Bonus Points options

  1. Add a delete to the stored procedure - benefit here is you can put this in a transaction
  2. Add a trigger that deletes the guest based on the insert into the client table - very effective if everyone is a guest before they are a customer

Point here is you don't have to solve this in MVC alone, since persistence is the real issue, not the user experience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM