当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Insert data into two build-in Auto-increment tables SQL-Server using Linq

问题描述:

This is my Database

I want to insert data into Bike and Contact simutaneously. Since Bike is dependent to Contact using ContactId, and the both PKs from the two tables is Auto-increment, I likely have to use ScopeIdentity. However, I'm developing a Windows Store app so the are no Sql Connection, WFC is pretty complicated, any other way to get it done?

This is my code so far in MainCreate Class

 var _scope_identity = (from contact in contacts

where contact.ContactId > 1

orderby contact.ContactId descending

select new { contact.ContactId }).First();

NewBike.ContactId = _scope_identity.ContactId;

I actually use .First() to get the latest record from Contact table. And then

 MainCreate mc = new MainCreate();

Binding b = new Binding();

b.Path = new PropertyPath("SelectedValue.NewBike.ContactId");

b.Mode = BindingMode.TwoWay;

b.ElementName = "grid_CreateBike";

if (tb_contactId.Text == null)

{

mc.GetScopeIdentity();

tb_contactId.Text = mc.NewBike.ContactId.ToString();

}

I think it is also a good way but inflexible. How can I implement that when people choose a Row in a Contacts ListView (for instant a contact with ContactId = 10), then click on a Add Bike button, the system will then recognize that a new Bike creating will be having a ContactId = 10?

Any solutions?

Thank you so much in advance!

分享给朋友:
您可能感兴趣的文章:
随机阅读: