码迷,mamicode.com
首页 > 其他好文 > 详细

【EF】疑难杂症

时间:2015-08-03 01:00:45      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:

用户和购物车数据  主从表

添加 ADO.NET实体数据类型

技术分享

 

  [Test]
        public void EntiyConnect()
        {
            var context = new projectDatabaseEntities();
            var objectcont = (context as IObjectContextAdapter).ObjectContext;
            
              Cart cart = new Cart(){ Goodsid = "商品id", Quantity = 2, User = new User(){Id = 1}};
              objectcont.AddObject("Cart", cart);

              objectcont.SaveChanges();      
        }
报错:
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 is mapped to a store-generated column. Column: ‘UserId‘.

 有人说:

在外键表一端,其Key 不能 是 自增标识字段。比如Office作为从表,其主键OfficeId就不能是自增的,否则EF会在向Office添加记录时报告:Adependent property in a ReferentialConstraint is mapped to a store-generatedcolumn.

 

http://www.cnblogs.com/liyanwei/p/280964fef29b58c8e95320fae3cbe4fa.html

来试一下:

修改数据user id为自增

更新一下

技术分享

   [Test]
        public void EntiyConnect()
        {
            var context = new projectDatabaseEntities();
            var objectcont = (context as IObjectContextAdapter).ObjectContext;
            User user= new User(){Userame = "zhangsan",UserAddress = "beijing  chaoyang"};
          //   Cart cart = new Cart(){ Goodsid = "商品id", Quantity = 2, User = new User(){Id = 1}};
            objectcont.AddObject("User", user);

              objectcont.SaveChanges();      
        }

这样做成功了(test两遍 第一遍id可能会是0),不要以为别人就说的对了,需要继续解决这个问题,Adependent property in a ReferentialConstraint is mapped to a store-generatedcolumn

 

还是stackoverflow 好用:Lhttp://stackoverflow.com/questions/6384659/a-dependent-property-in-a-referentialconstraint-is-mapped-to-a-store-generated-c

技术分享

问题出在

CREATE TABLE [dbo].[Cart] (
    [Id]       INT             NOT NULL,
    [Goodsid]  NVARCHAR (100) NOT NULL,
    [Quantity] INT            NOT NULL,
    [UserId]   INT           IDENTITY (1, 1) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

改为

CREATE TABLE [dbo].[Cart] (
    [Id]       INT            IDENTITY (1, 1) NOT NULL,
    [Goodsid]  NVARCHAR (100) NOT NULL,
    [Quantity] INT            NOT NULL,
    [UserId]   INT            NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

 

是不是和user主键设置了自增有关,没有验证。  已验证  没有关系。

 

接下来循环100次插入100条数据到cart。

 [Test]
        public void EntiyConnect()
        {
            var context = new projectDatabaseEntities();
            var objectcont = (context as IObjectContextAdapter).ObjectContext;
          

            for (int i = 0; i < 100; i++)
            {
                Cart cart = new Cart() { Goodsid = "商品id"+i, Quantity = 2, UserId = 1 };
                objectcont.AddObject("Cart", cart);  
            }
            objectcont.SaveChanges();  
        }
SaveChanges在for内外的性能怎么看呢?

删除
 [Test]
        public void DeleteCartByGoodsid()
        {
            var context = new projectDatabaseEntities();    var cartgoodsid = from c in context.Cart where c.Goodsid.Contains("2") select c;
            foreach (var b in cartgoodsid)
            {
                context.Cart.Remove(b);
            }         
            context.SaveChanges();
        }
 [Test]
        public void DeleteCartByGoodsid()
        {
            var context = new projectDatabaseEntities();
            var objectcont = (context as IObjectContextAdapter).ObjectContext;
            var cartgoodsid = from c in context.Cart where c.Goodsid.Contains("1") select c;
            foreach (var b in cartgoodsid)
            {
                objectcont.DeleteObject(b);
            }         
            context.SaveChanges();

        }

 

ObjectContext是不已经过时了?  不用它也完全可以处理 projectDatabaseEntities的对象要比ObjectContext丰富的多


  [Test]
        public void AddToCart2()
        {
            var context = new projectDatabaseEntities();
            Cart cart = new Cart() { Goodsid = "商品id", Quantity = 2, UserId = 1 };
            context.Cart.Add(cart);
            context.SaveChanges();
        }

5.x  估计可以不用管了 

技术分享

 

 

 

 

 [Test]
        public void GetCart()
        {
            var edm = new projectDatabaseEntities();
            var result1 = edm.Cart.Find(1);

            Console.WriteLine("edm.Cart.Find:" + result1.Goodsid);
           

            var list = (from c in edm.Cart select c ).Take(10);
            foreach (var user in list)
            {
                Console.WriteLine("(from c in edm.Cart select c ).Take(10):" + user.Goodsid);
            }

        }

 

 

edm.Cart.Find:sdfsdfsdfsd
(from c in edm.Cart select c ).Take(10):sdfsdfsdfsd
(from c in edm.Cart select c ).Take(10):sdfsdfsdfsd
(from c in edm.Cart select c ).Take(10):dfgdgd
(from c in edm.Cart select c ).Take(10):商品id
(from c in edm.Cart select c ).Take(10):商品id
(from c in edm.Cart select c ).Take(10):商品id
(from c in edm.Cart select c ).Take(10):商品id
(from c in edm.Cart select c ).Take(10):商品id
(from c in edm.Cart select c ).Take(10):商品id0
(from c in edm.Cart select c ).Take(10):商品id3

 

---怎样find别的字段呢 

--其实就是lambda表达式

  /// <summary>
        /// lambda 表达式
        /// </summary>
        [Test]
        public void GetCart()
        {
            var edm = new projectDatabaseEntities();

            var cart = edm.Cart.Select(p =>p.Goodsid);
            var result1 = cart.Where(p => p.Contains("5"));
            
            foreach (var b in result1)
            {
                Console.WriteLine("edm.Cart.Find:" + b);  
            }      
        }

 

【EF】疑难杂症

标签:

原文地址:http://www.cnblogs.com/viewcozy/p/4697149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!