0

わかりました、それで私は少し混乱しています。今朝まで(昨日最後に実行してから何も変更せずに)実行しようとしたmvc 4アプリケーションが数週間正常に動作していましたが、このエラーが表示されます。 EntityType'MyViewModel'にはキーが定義されていません。このEntityTypeのキーを定義します。ここで私の汎用リポジトリクラスにエラーが表示されます:

public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

私のアプリケーションはEFコードファーストメソッドを使用してデータベースを生成します。テーブル定義を見ると、IDプロパティが主キーとして認識されるため、ここで何が起こっているのかわかりません。

GenericRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data;
using SourceMvc.Models;
using SourceMvc.DAL;
using System.Linq.Expressions;

namespace SourceMvc.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
    internal StqmContext context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(StqmContext context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }
    public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = dbSet.Find(id);
        Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (context.Entry(entityToDelete).State == EntityState.Detached)
        {
            dbSet.Attach(entityToDelete);
        }
        dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        context.Entry(entityToUpdate).State = EntityState.Modified;
    }

}
}

私のビューモデル:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SourceMvc.Models;

namespace SourceMvc.DAL
{
public class QuotesViewModel
{
    //properties
    public General_Info     General_Info    { get; private set; }

    // Constructors

    public QuotesViewModel()
    {

    }

    public QuotesViewModel(General_Info general_info)
    {
        General_Info = general_info;
    }

そして、エラーがスローされたときに呼び出されるコントローラー:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SourceMvc.Models;
using SourceMvc.DAL;

namespace SourceMvc.Controllers
{
    public class VendorController : Controller
    {

        UnitOfWork unitOfWork = new UnitOfWork();

        internal IGeneral_Info_Repository general_info_repository;

        public VendorController()
        {
            this.general_info_repository = new General_Info_Repository(new StqmContext());
        }

        internal VendorController(IGeneral_Info_Repository general_info_repository)
        {
            this.general_info_repository = general_info_repository;
        }

        //
        // GET: /Vendor/

        [HttpGet]
        public ActionResult InitiateQuote()
        {
            return View();

        }

        //
        // POST: /Vendor/
        [HttpPost]
        public ActionResult InitiateQuote(int id = 0)
        {
            /*
             * If the form is filled out correctly 
             * (i.e. an existing quote id # is entered)
             * redirect the user to Vendor/Details/id
             */
            if (ModelState.IsValid)
            {
                int ids = id;
                return RedirectToAction("Overview", "Vendor", new { id = ids });
            }

            return View();
        }

        ////WHEN I SUBIT THE QUOTE ID AND THE OVERVIEW PAGE IS SUPPOSED TO LOAD,
        ///THAT'S WHEN I GET THE ERROR
        //
        // GET: /Vendor/Overview/id

        public ActionResult Overview(int id = 0)
        {
            General_Info general_info = unitOfWork.General_Info_Repository.GetByID(id);
            return View(new QuotesViewModel(general_info));
        }

私が言ったように、これは昨日機能し、何週間も機能しているので、私は正直にこれを引き起こしている原因がわかりません。助けようとする人には事前に感謝します。

そして、ここに表があります:

CREATE TABLE [dbo].[General_Info] (
[ID]              INT            IDENTITY (1, 1) NOT NULL,
[Open_Quote]      DATETIME       NOT NULL,
[Customer_Name]   NVARCHAR (MAX) NULL,
[OEM_Name]        NVARCHAR (MAX) NULL,
[Qty]             INT            NOT NULL,
[Quote_Num]       NVARCHAR (MAX) NULL,
[Fab_Drawing_Num] NVARCHAR (MAX) NULL,
[Rfq_Num]         NVARCHAR (MAX) NULL,
[Rev_Num]         NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.General_Info] PRIMARY KEY CLUSTERED ([ID] ASC)
);
4

1 に答える 1

3

この場合、ビューモデルまたはSQLテーブルの主キー制約を変更した可能性があると思います。EFで生成されたモデルでFind()を使用するには、主キーを定義する必要があります。

于 2013-03-22T16:26:01.613 に答える