C# 尝试使用EF执行插入时,实体具有一些无效参数
我找不到使用EF执行insert语句的方法 出现了许多错误 代码都在这里,问题在于控制器:我已经试过了:C# 尝试使用EF执行插入时,实体具有一些无效参数,c#,entity-framework,C#,Entity Framework,我找不到使用EF执行insert语句的方法 出现了许多错误 代码都在这里,问题在于控制器:我已经试过了: context.sistema_UsersCertified.Add(uc); context.Set<sistema_UsersCertified>().Add(uc); 我有一个向控制器发送数据的视图: public partial class tgpwebgedEntities : DbContext { public tgpwebgedEntities()
context.sistema_UsersCertified.Add(uc);
context.Set<sistema_UsersCertified>().Add(uc);
我有一个向控制器发送数据的视图:
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; }
}
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();
}
哦,我认为可能是您正在创建一个UsersCertified类型的模型,然后试图将其保存为sistema_UsersCertified类型的数据库实体 试一试
我只会使用context.sistema_UsersCertified.Add(uc);这是一个异常还是一个生成错误-错误的确切文本是什么?什么是
sistema\u用户认证的?可能必须是DbSet
?
sistema_UsersCertified dbEntity = new sistema_UsersCertified();
dbEntity.CertType = ...
context.sistema_UsersCertified.Add(dbEntity);