[英]How do i get the auto id from entity framework
I am wanting to get the id of the customer i just created to insert into the userId table this will be filled with information contained in a lookup table. 我想获取我刚刚创建的要插入到userId表中的客户的ID,该ID将由查找表中包含的信息填充。
My question is how do i retreive the id from the newewly created customer. 我的问题是如何从刚创建的客户那里获取ID。 I am using entity framework 6 btw 我正在使用实体框架6 btw
tblPortalCustomerInfo _customer = new tblPortalCustomerInfo();
_customer.firstName = firstname;
_customer.middleName = middle;
_customer.lastName = lastname;
_customer.IVACODE = ivaCode;
_customer.email = emailAddress;
portalEntities.tblPortalCustomerInfoes.Add(_customer);
tblPortalUser _user = new tblPortalUser();
_user.customerInfo = _customer.id; this is where i need the link?.
_user.EmailAddress = emailAddress;
_user.password = password;
_user.isActive = true;
_user.optinDateStart = DateTime.Now;
portalEntities.tblPortalUsers.Add(_user);
portalEntities.tblPortalUsers.Add(_user);
You have to insert that customer row first, and then it will autmoatically get populated with primary key value in _customer.id
generated for it like: 您必须先插入该客户行,然后自动为它生成的_customer.id
主键值填充该行:
tblPortalCustomerInfo _customer = new tblPortalCustomerInfo();
_customer.firstName = firstname;
_customer.middleName = middle;
_customer.lastName = lastname;
_customer.IVACODE = ivaCode;
_customer.email = emailAddress;
portalEntities.tblPortalCustomerInfoes.Add(_customer);
portalEntities.SaveChanges(); // insert the customer
tblPortalUser _user = new tblPortalUser();
_user.customerInfo = _customer.id; // now you should have primary key value here
_user.EmailAddress = emailAddress;
_user.password = password;
_user.isActive = true;
_user.optinDateStart = DateTime.Now;
添加新记录后,需要调用SaveChange()方法,然后将获得新生成的ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.