0

EFを使用して挿入ステートメントを実行する方法が見つかりません。

多くのエラーが発生しています。

コードはすべてここにあり、問題はコントローラーにあります:私はすでに試しました:

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

sistema_UsersCertifiedにはいくつかの無効な引数があると書かれています

なぜ?

これは私の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; }

}

このテーブルには、id(int)、userID(Guid)、certTypeID(int)、keyNumber(string)の4つの列があります。

そのため、そのテーブルに基づいてモデルを作成しました。

   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();
}
4

1 に答える 1

1

ああ、UsersCertifiedタイプのモデルを作成し、それをsistema_UsersCertifiedタイプのデータベースエンティティとして保存しようとしている可能性があると思います。

試す

sistema_UsersCertified dbEntity = new sistema_UsersCertified();
dbEntity.CertType = ...
context.sistema_UsersCertified.Add(dbEntity);
于 2012-11-30T08:37:08.160 に答える