Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While creating an e-commerce app I am trying to update buyer from buyer table and its address from location table. In short I am trying to update multiple models using view Model. I am confused with lamda expression.

Plus the foreachloop here is creating problem.
The error says "
An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: Unable to create a constant value of type 'InventorySystem.Models.Buyer'. Only primitive types or enumeration types are supported in this context.
"

What I have tried:

I have tried
var buyers = _context.Buyers.Join(_context.Locations, d => d.LocationId, o => o.id, (d, o) => new { c.Location, c.Buyer }).AsEnumerable();
            var viewModel = from l in _context.Locations
                            join b in _context.Buyers
                            on l.id equals (b.LocationId)
                            select new CustomerFormViewModel { Buyer = c.Buyer, Location = c.Location };
            foreach (var v in buyers)
            {
                v.Buyer.FName = c.Buyer.FName;
                v.Buyer.MName = c.Buyer.MName;
                v.Buyer.LName = c.Buyer.LName;
                v.Buyer.MembershipTypeId = c.Buyer.MembershipTypeId;
                v.Buyer.Verified = c.Buyer.Verified;
                v.Location.address = c.Location.address;
                v.Location.city = c.Location.city;
                v.Location.email = c.Location.email;
                v.Location.phone = c.Location.phone;
                v.Location.zipcode = c.Location.zipcode;

            }
_context.savechanges
Posted
Updated 25-Jan-17 0:32am
v2
Comments
Emrik Dml 25-Jan-17 6:30am    
While executing foreach loop I get following error
"An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: Unable to create a constant value of type 'InventorySystem.Models.Buyer'. Only primitive types or enumeration types are supported in this context."
sonymon mishra 25-Jan-17 7:54am    
on which line it is showing error?

1 solution

var viewModel = from l in _context.Locations
                            join b in _context.Buyers
                            on l.id equals (b.LocationId)
                            select new  { Buyer = c.Buyer, Location = c.Location };
            foreach (var v in viewModel)
            {
                v.Buyer.FName = c.Buyer.FName;
                v.Buyer.MName = c.Buyer.MName;
                v.Buyer.LName = c.Buyer.LName;
                v.Buyer.MembershipTypeId = c.Buyer.MembershipTypeId;
                v.Buyer.Verified = c.Buyer.Verified;
                v.Location.address = c.Location.address;
                v.Location.city = c.Location.city;
                v.Location.email = c.Location.email;
                v.Location.phone = c.Location.phone;
                v.Location.zipcode = c.Location.zipcode;
            }
            _context.SaveChanges();//This will update your changes to the database
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900