2013-03-18 106 views
0

DNN模块开发和使用MVC和Linq的新功能。已经建立了一个类和控制器,允许我在数据库的表中创建记录。任何人都可以告诉我检索新创建记录的ID的最佳方法吗?下面是创建记录的控制器部分。获取DNN模块新记录的ID

class BlockController 
{ 
    public void CreateBlock(Block b) 
    { 
     using (IDataContext ctx = DataContext.Instance()) 
     { 
      var rep = ctx.GetRepository<Block>(); 
      rep.Insert(b); 
     } 
    } 
} 

呼叫控制器从代码

  var bC = new BlockController(); 
      var b = new Block() 
      { 
       SectionId = int.Parse(ddlPickSection.SelectedValue), 
       PlanId = int.Parse(ddlPickPlan.SelectedValue), 
       BlockName = bId, 
       BlockDesc = "", 
       xPos = bX, 
       yPos = bY, 
       width = arrBWidths[i], 
       ModuleId = ModuleId, 
       CreatedOnDate = DateTime.Now, 
       CreatedByUserId = UserId, 
       LastModifiedOnDate = DateTime.Now, 
       LastModifiedByUserId = UserId, 
      }; 

      bC.CreateBlock(b); 

感谢

回答

1

当您提交在B对象的变化(插入DB记录)的ID将获得:

... 
rep.InsertOnSubmit(b); 
ctx.SubmitChanges(); 

int desireID = b.id;