3

I have extended a entity framework class (SQL server table) and added some extra properties to the child class but when I want to insert into the table which I already extended I get this exception:

Mapping and metadata information could not be found for EntityType 'Student.Models.Add.SubjectToStageModel'.

My controller:

[HttpPost]
public ActionResult SubjectToStage(SubjectToStageModel model)
{
    try
    {
        if (ModelState.IsValid)
        {
            using (StudentEntities studentEntities = new StudentEntities())
            {
                int intCount =
                    studentEntities.SubjectToStageTbls.Count(
                        x => x.StageId == model.StageId && x.SubjectId == model.SubjectId);
                if (intCount == 0)
                {
                    studentEntities.SubjectToStageTbls.Add(model);
                    studentEntities.SaveChanges();                           
                }
            }
        }
    }
    catch (Exception exception)
    {
        Debug.WriteLine(exception.ToString());
        TempData["error"] = "An error occured";
    }
    return RedirectToAction("SubjectToStage");
}

My base class:

public partial class SubjectToStageTbl
{
    public SubjectToStageTbl()
    {
        this.StudentMarkTbls = new HashSet<StudentMarkTbl>();
    }

    public int SubjectToStageId { get; set; }
    public int SubjectId { get; set; }
    public int StageId { get; set; }
    public int Point { get; set; }

    public virtual StageTbl StageTbl { get; set; }
    public virtual ICollection<StudentMarkTbl> StudentMarkTbls { get; set; }
    public virtual SubjectTbl SubjectTbl { get; set; }
}

My subclass:

public class SubjectToStageModel : SubjectToStageTbl
{
    public IEnumerable<SelectListItem> StageListItem
    {
        get
        {
            List<SelectListItem> listsSelectListItems = new List<SelectListItem>();
            try
            {
                using (StudentEntities studentEntities = new StudentEntities())
                {
                    IQueryable<StageTbl> queryableStage = studentEntities.StageTbls;
                    foreach (var stage in queryableStage)
                    {
                        SelectListItem selectListItem = new SelectListItem();
                        selectListItem.Value = stage.StageId.ToString();
                        selectListItem.Text = stage.StageName;
                        listsSelectListItems.Add(selectListItem);
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.ToString());
            }
            return listsSelectListItems;
        }
    }

    public IEnumerable<SelectListItem> SubjectListItem
    {
        get
        {
            List<SelectListItem> listsSelectListItems = new List<SelectListItem>();
            try
            {
                using (StudentEntities studentEntities = new StudentEntities())
                {
                    IQueryable<SubjectTbl> queryableSubject = studentEntities.SubjectTbls;
                    foreach (var stage in queryableSubject)
                    {
                        SelectListItem selectListItem = new SelectListItem();
                        selectListItem.Value = stage.SubjectId.ToString();
                        selectListItem.Text = stage.SubjectName;
                        listsSelectListItems.Add(selectListItem);
                    }
                }
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.ToString());
            }
            return listsSelectListItems;
        }
    }
}
1
  • 1
    What exactly is your question? Stating a bunch of facts, and then expecting us to figure out what you want to know is not a good way to ask a question. Commented Feb 8, 2015 at 1:30

1 Answer 1

1

You didn't explicitly map the SubjectToStageModel class. If you want Entity Framework to work with derived classes you should add them to the model as well. But I don't think you intended to do that in the first place.

In fact, SubjectToStageModel is a view model. It may look convenient to derive a view model from an entity class, but I think generally it's not a good idea. View models should be tailored to the view (or use case) they're used in. A couple of reasons:

  • It's very likely that the entity class contains more properties than you need in the view. In later maintenance it's always a pain to keep checking what you do and don't need.
  • While view evolves, it may require a differently structured model than the entity.
  • The view may require different validations.
  • The view may be allowed to return a state that absolutely shouldn't be stored (you may require some post processing of entered data), so it's good to ensure it can't possibly be stored.
  • It creates a dependency between the data layer model and the view.

Maybe these consideration don't apply in your case. Still I'd prefer to have an independent view model. However, if you're lazy (we developers prefer the word pragmatic), you may succeed by doing:

studentEntities.SubjectToStageTbls.Add((SubjectToStageTbl)model);

(I never tried though).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.