我找不到使用 EF 执行插入语句的方法。
出现了很多错误。
代码都在这里,问题出在控制器上:我已经尝试过:
context.sistema_UsersCertified.Add(uc);
context.Set<sistema_UsersCertified>().Add(uc);
它说 sistema_UsersCertified 有一些无效的参数
为什么?
这是我的数据库上下文:
public partial class tgpwebgedEntities : DbContext
{
public tgpwebgedEntities()
: base("name=tgpwebgedEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<sistema_UsersCertified> sistema_UsersCertified { get; set; }
}
该表有 4 个列:id(int)、userID(Guid)、certTypeID(int)、keyNumber(string)
所以我在该表中创建了一个模型:
public class UsersCertified
{
public Guid userId;
public int certTypeId;
public string keyNumber;
public UsersCertified(Guid uId, int ctId, string kn) {
userId = uId;
certTypeId = ctId;
keyNumber = kn;
}
}
我有一个将数据发送到控制器的视图:
<table>
<tr>
<td style="font-size:10px;">Habilitar Login com Cert. Digital:</td>
<td>@Html.CheckBoxFor(m => m.isCertified)</td>
</tr>
<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;
Guid uID = userID.First();
UsersCertified uc = new UsersCertified(uID, model.certType, model.identifier);
//HERE I NEED SET MY TABLE WITH MY MODEL... BUT SO MANY ERROR
context.SaveChanges();
}