【问题标题】:Cant Add controller which uses Class which is inherited from other class无法添加使用从其他类继承的类的控制器
【发布时间】:2012-09-25 02:18:44
【问题描述】:

我正在使用 Entity Framework 和 MVC3,我的问题是如果该类继承自另一个类,我将无法构建控制器。

例子:

这是基类

using System;
using System.Collections.Generic;

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

    }

这是领导班(孩子)

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


    }


}

当我尝试在下面添加控制器时出现错误...

上下文类代码

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

【问题讨论】:

  • 我们需要更多关于您的DbContext的信息
  • 我已经添加了上下文类代码。请检查我的问题。
  • public DbSet&lt;Lead&gt; Leads { get; set; } 添加到CRMWebContainer。这可能对你有用。

标签: asp.net-mvc entity-framework entity-framework-4 entity-framework-4.1 entity-relationship


【解决方案1】:

MVC AddController 窗口检查您正在添加的 ModelType 的属性 DbSet。 你应该像 vicentedealencar 说的那样,添加到你的CRMWebContainer

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

请记住,您不应在代码中使用此属性(这就是过时属性的原因),因为获取潜在客户的正确方法是使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2013-02-09
    • 1970-01-01
    • 2013-02-20
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多