不添加使用继承自其他类的Class的控制器 [英] Cant Add controller which uses Class which is inherited from other class

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

问题描述

我正在使用Entity Framework和MVC3,而我的问题是,如果类继承自另一个类,我不能支持控制器。



示例: p>

这是基类

  using System; 
使用System.Collections.Generic;

命名空间CRMEntities
{
public partial class Company
{
public int Id {get;组; }
}

}

这是铅班)

  using System; 
使用System.Collections.Generic;

命名空间CRMEntities
{
public partial class Lead:Company
{
public Lead()
{
this.Status = 1;
this.IsQualified = false;

}

public Nullable< short>状态{get;组; }
public Nullable< bool>已获得组; }


}


}

当我尝试添加控制器下面的错误...





上下文类CODE

 code> using System; 
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;

命名空间CRMEntities
{
public partial class CRMWebContainer:DbContext
{
public CRMWebContainer()
:base(name = CRMWebContainer )
{
}

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

public DbSet< Employee>员工{get;组; }
public DbSet< Contact>联系人{get;组; }
public DbSet< Product>产品{get;组; }
public DbSet< Location>地点{get;组; }
public DbSet< Task>任务{get;组; }
public DbSet< EventInfo> EventInfoes {get;组; }
public DbSet<机会>机会{get;组; }
public DbSet< Comment>评论{get;组; }
public DbSet< Document>文件{get;组; }
public DbSet< LoginInformation> LoginInformations {get;组; }
public DbSet< CRMLog> CRMLogs {get;组; }
public DbSet< EntitySharing> EntitySharings {get;组; }
public DbSet< EntityFlagging> EntityFlaggings {get;组; }
public DbSet< EntityTagging> EntityTaggings {get;组; }
public DbSet< EntitySubscribing> EntitySubscribings {get;组; }
public DbSet< Compapny>合并{get;组;
}
}


解决方案

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

  [Obsolete(Design only,true)] 
public DbSet< Lead>引导{get;组;记住你不应该在你的代码中使用这个属性(这就是为什么Obsolete Attribute),因为获得潜在客户的正确方法是使用:

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


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

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

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