User and shopping cart data master-Slave table
Add ADO. NET Entity Data types
[Test] Public voidEntiyconnect () {varContext =Newprojectdatabaseentities (); varObjectcont = (context asiobjectcontextadapter). ObjectContext; Cart Cart=NewCart () {goodsid ="Product ID", Quantity =2, User =NewUser () {Id =1}}; Objectcont. AddObject ("Cart", cart); Objectcont. SaveChanges (); }
Error:
System.Data.Entity.Core.UpdateException:An error occurred while updating the entries. See the inner exception for details. ----> System.invalidoperationexception:a Dependent property in a referentialconstraint are mapped to a store-generated Column. Column: ' UserId '.
Some people say:
At the foreign key table, its key cannot be a self-increment identity field. For example, office as a slave from a table, its primary key OfficeId cannot be self-increasing, otherwise EF will report when adding records to office: Adependent-A referentialconstraint is mapped to a Store-generatedcolumn.
Http://www.cnblogs.com/liyanwei/p/280964fef29b58c8e95320fae3cbe4fa.html
To try it out:
Modified data user ID is self-increment
Update a bit
[Test] Public voidEntiyconnect () {varContext =Newprojectdatabaseentities (); varObjectcont = (context asiobjectcontextadapter). ObjectContext; User User=NewUser () {userame ="Zhangsan", useraddress ="Beijing Chaoyang"}; //cart cart = new cart () {goodsid = "product Id", Quantity = 2, user = new User () {Id = 1}};Objectcont. AddObject ("User", user); Objectcont. SaveChanges (); }
This is successful (test two times the first pass ID can be 0), do not think that others are right, need to continue to solve this problem, adependent property in a referentialconstraint is mapped to a Store-generatedcolumn
It's still stackoverflow. Useful: lhttp://stackoverflow.com/questions/6384659/ A-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c
The problem is
CREATE TABLE [dbo].[Cart] ( [Id] INT not NULL, [Goodsid] NVARCHAR( -) not NULL, [Quantity] INT not NULL, [UserId]INTIDENTITY (1, 1) not NULL, PRIMARY KEY CLUSTERED([Id] ASC));
Switch
CREATE TABLE [dbo].[Cart] ( [Id] INT IDENTITY(1,1) not NULL, [Goodsid] NVARCHAR( -) not NULL, [Quantity] INT not NULL, [UserId] INT not NULL, PRIMARY KEY CLUSTERED([Id] ASC));
is not associated with the user primary key set, there is no validation. No relationship has been verified.
Then loop 100 times to insert 100 data into the cart.
[Test] Public voidEntiyconnect () {varContext =Newprojectdatabaseentities (); varObjectcont = (context asiobjectcontextadapter). ObjectContext; for(inti =0; I < -; i++) {cart cart=NewCart () {goodsid ="Product ID"+i, Quantity =2, UserId =1 }; Objectcont. AddObject ("Cart", cart); } objectcont. SaveChanges (); }
What about the performance of SaveChanges in for and outside?
Delete
[Test] Public voidDeletecartbygoodsid () {varContext =Newprojectdatabaseentities (); varCartgoodsid = fromCinchContext. CartwhereC.goodsid.contains ("2")SelectC; foreach(varBinchCartgoodsid) {context. Cart.remove (b); } context. SaveChanges (); }
[Test] Public voidDeletecartbygoodsid () {varContext =Newprojectdatabaseentities (); varObjectcont = (context asiobjectcontextadapter). ObjectContext; varCartgoodsid = fromCinchContext. CartwhereC.goodsid.contains ("1")SelectC; foreach(varBinchCartgoodsid) {Objectcont. DeleteObject (b); } context. SaveChanges (); }
ObjectContext is not out of date? projectdatabaseentities objects are much richer than ObjectContext.
[Test] publicvoid AddToCart2 () { varNew projectdatabaseentities (); New " Product ID " 2 1 }; Context. Cart.add (cart); Context. SaveChanges (); }
5.x's not going to make it.
[Test] Public voidGetcart () {varEDM =Newprojectdatabaseentities (); varRESULT1 = EDM. Cart.find (1); Console.WriteLine ("EDM. Cart.find:"+RESULT1. GOODSID); varList = ( fromCinchEdm. CartSelectc). Take (Ten); foreach(varUserinchlist) {Console.WriteLine ("(from the C in EDM.) Cart select C). Take (Ten):"+user. GOODSID); } }
Edm. Cart.Find:sdfsdfsdfsd
(from the C in EDM.) Cart select C). Take (Ten): SDFSDFSDFSD
(from the C in EDM.) Cart select C). Take (Ten): SDFSDFSDFSD
(from the C in EDM.) Cart select C). Take (Ten):d FGDGD
(from the C in EDM.) Cart select C). Take (10): Product ID
(from the C in EDM.) Cart select C). Take (10): Product ID
(from the C in EDM.) Cart select C). Take (10): Product ID
(from the C in EDM.) Cart select C). Take (10): Product ID
(from the C in EDM.) Cart select C). Take (10): Product ID
(from the C in EDM.) Cart select C). Take (10): Product ID0
(from the C in EDM.) Cart select C). Take (10): Product ID3
How do---find other fields?
--it's actually a lambda expression.
/// <summary> ///lambda expression/// </summary>[Test] Public voidGetcart () {varEDM =Newprojectdatabaseentities (); varCart = EDM. Cart.select (p =p.goodsid); varRESULT1 = cart. Where (p = p.contains ("5")); foreach(varBinchresult1) {Console.WriteLine ("EDM. Cart.find:"+b); } }
"EF" incurable diseases