Cant Add controller which uses Class which is inherited from other class

▼魔方 西西 提交于 2020-01-11 06:45:26

问题


I'm using Entity Framework and MVC3, and my problem is that I can't scaffold Controllers if the class inherits from another Class.

Example:

This is Base Class

using System;
using System.Collections.Generic;

    namespace CRMEntities
    {
        public partial class Company
        {
            public int Id { get; set; }
        }

    }

This is Lead Class (Child)

using System;
using System.Collections.Generic;

namespace CRMEntities
{
    public partial class Lead : Company
    {
        public Lead()
        {
            this.Status = 1;
            this.IsQualified = false;

        }

        public Nullable<short> Status { get; set; }
        public Nullable<bool> IsQualified { get; set; }


    }


}

When I tried to add controller below error comes...

Context Class COde

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace CRMEntities
{
    public partial class CRMWebContainer : DbContext
    {
        public CRMWebContainer()
            : base("name=CRMWebContainer")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<Contact> Contacts { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Task> Tasks { get; set; }
        public DbSet<EventInfo> EventInfoes { get; set; }
        public DbSet<Opportunity> Opportunities { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<Document> Documents { get; set; }
        public DbSet<LoginInformation> LoginInformations { get; set; }
        public DbSet<CRMLog> CRMLogs { get; set; }
        public DbSet<EntitySharing> EntitySharings { get; set; }
        public DbSet<EntityFlagging> EntityFlaggings { get; set; }
        public DbSet<EntityTagging> EntityTaggings { get; set; }
        public DbSet<EntitySubscribing> EntitySubscribings { get; set; }
        public DbSet<Compapny> Compapnies { get; set; }
    }
}

回答1:


The MVC AddController window check for a property DbSet of the ModelType you are adding. You should do like vicentedealencar said, add to your CRMWebContainer:

[Obsolete("Design only", true)]
public DbSet<Lead> Leads { get; set; }

Remember that u should not use this property in your code (this is why the Obsolete Attribute), since the right way to get the Leads is using:

var leads = Companies.OfType< Lead >();


来源:https://stackoverflow.com/questions/12720611/cant-add-controller-which-uses-class-which-is-inherited-from-other-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!