[RESOLVED]Unsupported context type in mvc 4?

Hi 

I have created MVC 4 application (RAZOR) in VS2010. I have added .edmx and a model as below 

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace MVC4.Models
{
    public class SrcSysModelList
    {
        public List<SrcSys> LstSysCollection { get; set; }
        public SrcSys ObjSrcSys { get; set; }
    }

    public class SrcSys
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "Please Enter Source System")]
        public string System { get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string ModifiedBy { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string CreatedBy { get; set; }
    }
}

In .edmx I have a table called WF_SourceSystem_backup, now when I try to add controller with read/write actions am getting error message as Unsupported context type. I have chosen Modal class = SrcSys (MVC4.Models)
and Data Context Class = WF_SourceSystem_backup (MVC4) How to solve this?

Thanks

Hi,

Try add "[Key]" before ID property of SrcSys class, and code to be like this:

    public class SrcSys
    {
        [Key]
        public int ID { get; set; }

        [Required(ErrorMessage = "Please Enter Source System")]
        public string System { get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string ModifiedBy { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string CreatedBy { get; set; }
    }

Have fun

The only thing that I can think of is that there is an issue with your actual DataContext class itself. Generally, if you are using Entity Framework, this class might be called something like "WF_SourceSystem_backupEntities" and this is going to be the context
that you are looking for.

Your Data Context class is going to need to be your top-level class that will access all of your data. As far as your Model goes, you should be fine with whatever you choose for that.

Hi

I added [Key] attribute but same problem.

@Williamams

The name of my edmx is AuthWF_DB.edmx, and I added a table called WF_SourceSystem_backup. What I have to do here and How to solve this?

Thanks

If you are using Entity Framework, you should be able to generate your classes through the following steps :

  • Open your EDMX model designer.
  • On the design surface Right Click –> Add Code Generation Item…
  • Select Online templates.
  • Select EF 6.x DbContext Generator for C# (or similar).
  • Click ‘Add’.

It’s a bit tough to discern exactly where you are in this process (e.g. if you already have your classes etc.). You should have some type of Entities or Context class that was generated from your database which is going to be what is used within
the Add Controller scaffolding as the Context class.

Would it be possible to post a screenshot of what your solution currently looks like? It might be easier for us to get some more detail about your project and be able to ask more specific questions.

Hi

Thanks for your reply. Here is my code

[assembly: EdmSchemaAttribute()]
namespace MVC4
{
    #region Contexts
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    public partial class AuthWorkFlowEntities1 : ObjectContext
    {
        #region Constructors
    
        /// <summary>
        /// Initializes a new AuthWorkFlowEntities1 object using the connection string found in the 'AuthWorkFlowEntities1' section of the application configuration file.
        /// </summary>
        public AuthWorkFlowEntities1() : base("name=AuthWorkFlowEntities1", "AuthWorkFlowEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new AuthWorkFlowEntities1 object.
        /// </summary>
        public AuthWorkFlowEntities1(string connectionString) : base(connectionString, "AuthWorkFlowEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        /// <summary>
        /// Initialize a new AuthWorkFlowEntities1 object.
        /// </summary>
        public AuthWorkFlowEntities1(EntityConnection connection) : base(connection, "AuthWorkFlowEntities1")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }
    
        #endregion
    
        #region Partial Methods
    
        partial void OnContextCreated();
    
        #endregion
    
        #region ObjectSet Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        public ObjectSet<WF_SourceSystem_backup> WF_SourceSystem_backup
        {
            get
            {
                if ((_WF_SourceSystem_backup == null))
                {
                    _WF_SourceSystem_backup = base.CreateObjectSet<WF_SourceSystem_backup>("WF_SourceSystem_backup");
                }
                return _WF_SourceSystem_backup;
            }
        }
        private ObjectSet<WF_SourceSystem_backup> _WF_SourceSystem_backup;

        #endregion

        #region AddTo Methods
    
        /// <summary>
        /// Deprecated Method for adding a new object to the WF_SourceSystem_backup EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
        /// </summary>
        public void AddToWF_SourceSystem_backup(WF_SourceSystem_backup wF_SourceSystem_backup)
        {
            base.AddObject("WF_SourceSystem_backup", wF_SourceSystem_backup);
        }

        #endregion

    }

    #endregion

    #region Entities
    
    /// <summary>
    /// No Metadata Documentation available.
    /// </summary>
    [EdmEntityTypeAttribute(NamespaceName="AuthWorkFlowModel", Name="WF_SourceSystem_backup")]
    [Serializable()]
    [DataContractAttribute(IsReference=true)]
    public partial class WF_SourceSystem_backup : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new WF_SourceSystem_backup object.
        /// </summary>
        /// <param name="id">Initial value of the ID property.</param>
        public static WF_SourceSystem_backup CreateWF_SourceSystem_backup(global::System.Int32 id)
        {
            WF_SourceSystem_backup wF_SourceSystem_backup = new WF_SourceSystem_backup();
            wF_SourceSystem_backup.ID = id;
            return wF_SourceSystem_backup;
        }

        #endregion

        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 ID
..................................
.........................

Your context is AuthWorkFlowEntities1  rather than
WF_SourceSystem_backup (which is an entity and not a context). What if you try with the proper context?

Hi Athar Ali Kh,

Thanks for your post.

According to your description,

Athar Ali Khan

Unsupported context type in mvc 4?

I guess that you have reference the classes incorrectly and  you need to make correct referencing appropriately.

  1. Reference Model class for model and
  2. For Data Context class for Data Context class.

Hope this can be helpful.

Best Regards,

Eileen

Athar Ali Khan

In .edmx I have a table called WF_SourceSystem_backup, now when I try to add controller with read/write actions am getting error message as Unsupported context type. I have chosen Modal class = SrcSys (MVC4.Models)
and Data Context Class = WF_SourceSystem_backup (MVC4) How to solve this?

You are going to want to continue to use your SrsSys class for your Model class, however you’ll need to
change your Data Context class to AuthWorkFlowEntities1.

Hi

@Williams, I did changed to AuthWorkFlowEntities1 but no use. I have changed my Model as below and it worked.

namespace MVC4
{
    [MetadataType(typeof(WF_SourceSystem_backup1))]
    public partial class WF_SourceSystem_backup
    {
       
    }

    internal sealed class WF_SourceSystem_backup1
    {
        [Key]
        public int ID { get; set; }

        [Required(ErrorMessage = "Please Enter Source System")]
        public string System { get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string CreatedBy { get; set; }

        [Required(ErrorMessage = "Please Enter BSF ID")]
        public string ModifiedBy { get; set; }
    }
}

Leave a Reply