简体   繁体   English

如何获取自动生成的ID?

[英]How do I get the auto generated Id?

System
Language:C#
Database:SQL
IDE:C# Express 2010

I have a User object/class and db table. 我有一个User对象/类和db表。 The user has multiple Adresses and I have already defined the Addresses table, how do I add and commit the address to the DB 用户有多个地址,我已经定义了地址表,如何添加地址并将其提交给数据库

I have created an Add function for the User (see below) The User class holds a List addresses 我为用户创建了一个Add函数(见下文)User类包含一个List地址

User Class 用户类

int id
string name
List<Address> adresses

Table User 表用户

id
name
addresses

Table Addresses 表地址

id 
address
user_id (FK)

I have the Add function in the User class to add User Objects, I stripped out the handling code to make it clearer on SO. 我在User类中添加Add函数来添加用户对象,我删除了处理代码以使其更清晰。

My question is, how do I get the id of the User just added and add the Address into the database. 我的问题是,如何获取刚刚添加的用户的ID并将地址添加到数据库中。 And How do I define the FK on the Address Table 以及如何在地址表上定义FK

 class User {

    public static bool Add(User user, UserOptions option = UserOptions.System)
    {
        //Instantiate the  DataClassesDataContext
        var db = DBContext.Create();

        //Make Sure User does not exist
        //returns true if no existing user
        if (Users.validateForAdd(user))
        {
           //Now add
           db.Users.InsertOnSubmit(user);

           //I assume I would get the id of the user just added but not sure how I do that

           db.SubmitChanges();
           return true;
        }

        //Failed to add
        return false;
    }

Final Solution 最终解决方案

Just thought I would post the final solution from the below answers and comments. 我想我会从下面的答案和评论中发布最终解决方案。

//
// Creating and setting the Objects
//
User user = new User(name="john doe");
EntitySet < Address >  adressList = new EntitySet<Address>();

adressList.Add(new Address("Residential Home");
adressList.Add(new Address("Holiday Home...");

user.address = adressList;

//
//Persisting to the DB
//
var db = new DataContext();
db.Results.InsertOnSubmit(result);
db.SubmitChanges();

You can do one change to the above method Add. 您可以对上面的方法添加一个更改。 Instead of returning bool, you can return type int. 您可以返回int类型,而不是返回bool。

public static int Add(User user, UserOptions option = UserOptions.System)
 //Make Sure User does not exist
 //returns true if no existing user
 if (Users.validateForAdd(user))
 {
    ....
    ....
    //Instead of returning true you can return user.id
    return user.id;
 }
    //Instead of returning false you can return default value -1
    return -1;

}

So you can get the newly added user's id by calling method Add of User class and you can use that id to create List of addresses for the user. 因此,您可以通过调用Add of User类的方法获取新添加的用户ID,并且可以使用该id为用户创建地址列表。

My question is, how do I get the id of the User just added and add the Address into the database. 我的问题是,如何获取刚刚添加的用户的ID并将地址添加到数据库中。

The key is that you do not need it . 关键是你不需要它 Instead of assigning a foreign key field with an id, in Linq-2-sql you assign entities to eachother. 在Linq-2-sql中,您可以将实体分配给彼此,而不是使用id分配外键字段。

So instead of doing 所以不要这样做

somerelated_table.fk_user_id = user.id

you do 你做

somerelated_table.user = user

Linq-2-sql will handle the proper assignment of the user.id when you call SubmitChanges. 当您调用SubmitChanges时,Linq-2-sql将处理user.id的正确分配。 Plus as a bonus it will all be done in a single transaction. 另外,作为奖励,它将在一次交易中完成。 That is the beauty of linq-2-sql. 这就是linq-2-sql的美妙之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM