Linq to Entities问题 [英] Linq to Entities Question

查看:83
本文介绍了Linq to Entities问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


我的数据库中有一个商家表。它有一个BusinessID作为其PK与SQl自动增量。


一切正常,但我对某事感到困惑。



Business NewBusiness = new Business() ;
NewBusiness.Name = BusinessNameTxt.Text;

BusinessActions Ba = new BusinessActions();


NewBusiness.BusinessID //它会给我一个自动编号。它在我插入记录之前知道这一点,这是如何工作的?。


如果i 5个网页同时插入一条记录,那将是多么美好。 linq会不会得到重复的数字?


这是一个有趣的问题。




谢谢


gary

解决方案

如果这是实体对象,只需实例化一个新对象在内存中不会填充BusinessID字段,除非您有一些已实现的自定义业务逻辑。

但是,如果您实例化一个新的NewBusiness对象,将其附加到objectCnotext然后调用SaveChanges,默认情况下EF的行为是它将带回数据库创建的新BusinessID并将其插入到NewBusiness对象中。在这种情况下,您没有冲突,因为它是创建ID的数据库,当您执行Linq to Entities查询时,它将从数据库中获取ID。

这是否有意义?这听起来有点令人费解......所以我可能需要尝试表达一点不同。 :-)

朱莉


 hi

I have a Business Table in my DB. it has a BusinessID as its PK with SQl auto Increment. 

All works fine however i am confused about something.

 

 Business NewBusiness = new Business();
        NewBusiness.Name = BusinessNameTxt.Text;
   


        BusinessActions Ba = new BusinessActions();

NewBusiness.BusinessID  // it will give me the auto number. It knows this before i insert the record, how does this work ?.

what happnes if i 5 webpages eatch inserting a record at the same time. will linq to entites not get duplicate numbers ?

this is a interesting question.

 

 

thanks

gary

解决方案

If this is entity objects, just instantiating a new object in memory will not populate the BusinessID field unless you have some custom business logic that you have implemented.

However, if you instantiate a new NewBusiness object, attache it to an objectCnotext and then call SaveChanges, the default behavior of EF is that it will bring back the new BusinessID created by the database and insert it into the NewBusiness object. In this case, you don't have a conflict because it is the database creating the ids and when you do a Linq to Entities query it will get the ids from the database.

Does that make sense? It sounds a little convoluted ...so I might need to try to express it a bit differently. :-)

Julie


这篇关于Linq to Entities问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆