不能添加控制器,它使用类,这是从其他类继承 [英] Cant Add controller which uses Class which is inherited from other class

本文介绍了不能添加控制器,它使用类,这是从其他类继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架和MVC3,我的问题是,我不能,如果类从另一个类继承。支架控制器

例如:

这是基类

 使用系统;
使用System.Collections.Generic;    命名空间CRMEntities
    {
        公共部分类公司
        {
            公众诠释标识{搞定;组; }
        }    }

这是铅类(儿童)

 使用系统;
使用System.Collections.Generic;命名空间CRMEntities
{
    公共部分类铅:公司
    {
        公共铅()
        {
            this.Status = 1;
            this.IsQualified = FALSE;        }        公众可空<短>状态{搞定;组; }
        公众可空<布尔> IsQualified {搞定;组; }
    }
}

当我尝试添加以下控制器错误出现...

上下文类code

 使用系统;
使用System.Data.Entity的;
使用System.Data.Entity.Infrastructure;命名空间CRMEntities
{
    公共部分类CRMWebContainer:的DbContext
    {
        公共CRMWebContainer()
            :基地(NAME = CRMWebContainer)
        {
        }        保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
        {
            抛出新的意外codeFirstException();
        }        公共DbSet<员工>员工{搞定;组; }
        公共DbSet<联系与GT;联系方式{搞定;组; }
        公共DbSet<产品与GT;产品{搞定;组; }
        公共DbSet<地点>位置{搞定;组; }
        公共DbSet<任务>任务{搞定;组; }
        公共DbSet< EventInfo> EventInfoes {搞定;组; }
        公共DbSet<机遇与GT;机会{搞定;组; }
        公共DbSet<&评论GT;评论{搞定;组; }
        公共DbSet<文件>文件{搞定;组; }
        公共DbSet< LoginInformation> LoginInformations {搞定;组; }
        公共DbSet< CRMLog> CRMLogs {搞定;组; }
        公共DbSet< EntitySharing> EntitySharings {搞定;组; }
        公共DbSet< EntityFlagging> EntityFlaggings {搞定;组; }
        公共DbSet< EntityTagging> EntityTaggings {搞定;组; }
        公共DbSet< EntitySubscribing> EntitySubscribings {搞定;组; }
        公共DbSet< Compapny> Compapnies {搞定;组; }
    }
}


解决方案

的MVC AddController窗口检查一个属性的ModelType的DbSet要添加。
你应该做的像vicentedealencar说,添加到您的 CRMWebContainer

  [过时(只设计,真)
公共DbSet<铅>信息{搞定;组; }

记住,美不应该在你的code使用这一属性(这就是为什么过时的属性),因为正确的方式来获取信息使用的是:

  VAR导致= Companies.OfType<铅>();

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; }
    }
}

解决方案

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

这篇关于不能添加控制器,它使用类,这是从其他类继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆