当尝试使用EF执行INSERT时,实体有一些无效参数 [英] entity has some invalid arguments when trying to perform an INSERT with EF

查看:109
本文介绍了当尝试使用EF执行INSERT时,实体有一些无效参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到使用EF执行插入语句的方式。

I cannot find a way to perform a insert statement using EF.

出现了许多错误。

代码全部在这里,问题是与控制器:我有alread尝试:

The code is all here and the problem is with the controller: I have alread tried:

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

它表示sistema_UsersCertified有一些无效的争论

It says that sistema_UsersCertified has some invalid argumments

为什么?

这是我的dbcontext:

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个collumns:id(int) userID(Guid),certTypeID(int),keyNumber(string)

This table has 4 collumns: 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();
}


推荐答案

哦,我觉得可能您正在创建一个类型为UsersCertified的模型,然后尝试将其另存为sistema_UsersCertified类型的数据库实体。

Oh I think it may be that you are creating a model of type UsersCertified and then trying to save it as a database entity of type sistema_UsersCertified.

尝试

sistema_UsersCertified dbEntity = new sistema_UsersCertified();
dbEntity.CertType = ...
context.sistema_UsersCertified.Add(dbEntity);

这篇关于当尝试使用EF执行INSERT时,实体有一些无效参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆