获取插入的ID

时间:2014-03-10 14:56:23

标签: c# entity-framework

我正在尝试将数据插入数据库。我在这里,它的工作原理,但我需要从我插入的数据中获取数据的ID。该ID将用于存储提醒的名称。

private void appBarButton_Click(object sender, EventArgs e)
    {
        using (DatabaseContext context = new DatabaseContext(ConnectionString))
        {
            ToDoList todolist = new ToDoList();
            todolist.Title = titleTBox.Text;
            todolist.Description = descriptionTBox.Text;

            //date was added later, datacontext is needed to regenerate
            //DateTime date = DateTime.Parse(textDateOutput.Text);
            Console.WriteLine(dateData.Value.ToString());
            todolist.Date = DateTime.Parse(dateData.Value.ToString());

            context.ToDoList.InsertOnSubmit(todolist);

            context.SubmitChanges();

            DateTime Date = rDate.Value.Value;
            TimeSpan Time = rTime.Value.Value.TimeOfDay;
            Date = Date.Date + Time;
            String Content = descriptionTBox.Text;
            String Title = titleTBox.Text;
                   // ID = get the ID from database of the inserted data.


            if (Convert.ToBoolean(cBox.IsChecked))
            {
                if (Date < DateTime.Now)
                    MessageBox.Show("Please do not enter datetime lower then the current datetime");
                else if (String.IsNullOrEmpty(Title))
                    MessageBox.Show("Please do not leave the title empty");
                else
                {
                    //gets the info and call the function to send the parameter
                    RegisterScheduleIfNotExist( /* ID */, Title, Content, Date);
                    NavigationService.GoBack();
                }
            }
            else
                NavigationService.GoBack();
        }
    }

RegisterScheduleIfNotExist方法

private void RegisterScheduleIfNotExist(string name, string title, string content, DateTime time)
    {
        ScheduledAction currentReminder = ScheduledActionService.Find(name);
        if (currentReminder != null)
        {
            ScheduledActionService.Remove(currentReminder.Name);
        }

        var reminder = new Reminder(name)
        {
            BeginTime = time,
            Title = title,
            Content = content,
        };

        ScheduledActionService.Add(reminder);
    }

1 个答案:

答案 0 :(得分:1)

当您在ToDoList课程中有一个ID字段时,它会在SubmitChanges之后填充并指定值。

您可以像这样获取ID:

context.ToDoList.InsertOnSubmit(todolist);
context.SubmitChanges();
int id = todolist.Id; //or whatever your ID field is called on the class

关于评论的编辑:

您的RegisterscheduleIfNOtExist()方法没有id的参数字段。

您应该添加一个(更改方法的签名以包含int id, string name, etc...

如果方法name参数是id字段,只需将int id = todolist.Id更改为string id = todolist.Id.ToString()

以下是要更改的方法示例:

private void RegisterScheduleIfNotExist(int id, string name, string title, string content, DateTime time) 
{   
    ScheduledAction currentReminder = ScheduledActionService.Find(name);

    if (currentReminder != null)
    {
        ScheduledActionService.Remove(currentReminder.Name);
    }

    var reminder = new Reminder(name)
    {
        Id = id,  //Add the id field here to the reminder
        BeginTime = time,
        Title = title,
        Content = content,
    };

    ScheduledActionService.Add(reminder);
}