0

这是我尝试插入数据的第一个我自己创建的表。

问题:在我的控制器中,我不确定如何将模型与实际的数据库表链接以执行INSERT.

同样在该行中,uc.userId = userId;我收到以下错误:

无效的初始化程序成员声明

我为这个表创建了一个模型:

public class RegisterModel
{
    public bool isCertified { get; set; }
    public int certType { get; set; }
    public string identifier { get; set; }
}

看法:

<tr>
   <td style="font-size:10px;">Habilitar Login com Cert. Digital:</td>
   <td>@Html.CheckBoxFor(m => m.isCertified)</td>
</tr>                                      
<div id="certDigitalBlock">
<tr>
    <td class="smallField">Tipo do Certificado:</td>
    <td>@Html.DropDownListFor(m => m.certType, new SelectList(ViewBag.certType, "id","type"))</td>
</tr>
<tr>
    <td>Identificação:</td>
    <td>@Html.TextBoxFor(m => m.identifier, new { @class = "validate[required]" })      @Html.ValidationMessage("awnserVal", "*")</td>
</tr> 
</table>

控制器

using (tgpwebgedEntities context = new tgpwebgedEntities())
{
   var userID = from u in context.aspnet_Users where u.UserName == model.UserName select u.UserId;

   if (checkIsCertified == true)
   {
      UsersCertified uc = new UsersCertified {
                            uc.userId = userId;
                            uc.certTypeId = model.certType;
                            uc.keyNumber = model.identifier;
                        }

      context.SaveChanges()
   }
}

谢谢

4

2 回答 2

1
UsersCertified uc = new UsersCertified {
                            uc.userId = userId;
                            uc.certTypeId = model.certType;
                            uc.keyNumber = model.identifier;
                        }

不应该写在括号里吗?如果您使用的是构造函数。

UsersCertified uc = new UsersCertified (
                            uc.userId = userId;
                            uc.certTypeId = model.certType;
                            uc.keyNumber = model.identifier;
                        )

添加此代码:

context.UsersCertified .InsertOnSubmit(uc);
                context.SubmitChanges();
于 2012-11-30T06:39:08.693 回答
1

我想你不见了

context.Set<UsersCertified>().Add(uc);

context.SaveChanges();

顺便说一句,您可能想阅读我关于存储库模式的答案。它可以帮助您了解如何使用数据库并按级别拆分应用程序。

于 2012-11-30T07:08:58.147 回答