Asp.net mvc 如何在asp.net web api的ef codefirst模型中创建自定义类

Asp.net mvc 如何在asp.net web api的ef codefirst模型中创建自定义类,asp.net-mvc,ef-code-first,asp.net-web-api,Asp.net Mvc,Ef Code First,Asp.net Web Api,我正在使用asp.NETWebAPI。我在现有数据库中使用EF4.1代码优先模型。我有一门课 public class Tab1 { public int ID{get; set;} public string FirstName{get; set;} public string LastName{get; set;} } public class Tab2:Tab1 { public fullname{get; set;} } 我创建了dbcontext,比如 public class E

我正在使用asp.NETWebAPI。我在现有数据库中使用EF4.1代码优先模型。我有一门课

public class Tab1
{
public int ID{get; set;}
public string FirstName{get; set;}
public string LastName{get; set;}
}
public class Tab2:Tab1
{
public fullname{get; set;}
}
我创建了dbcontext,比如

public class EFBarContext:DbContext
    {
        public DbSet<Tab1> User{ get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Tab1>().ToTable("User");
        }
    }
为此,我创建了一个自定义类

public class Tab1
{
public int ID{get; set;}
public string FirstName{get; set;}
public string LastName{get; set;}
}
public class Tab2:Tab1
{
public fullname{get; set;}
}
我返回带有
list
的列表,但我得到一个错误,如
指定的架构无效错误0064:不能为类型“mediumtext”指定Facet“MaxLength”。

我一直在关注下面的帖子,但在我的情况下我无法理解。所以请指导我。

您的fullname属性是从firstname和lastname派生的,因此我不会在db中创建单独的表来存储它(这就是您使用
Tab2:Tab1
类所做的)

相反,您可以在webapi控制器中派生此字段:

using (var context = new EFBarContext())
{
    var jsonObject = context.User.Select(x => new {
        ID = x.ID,
        firstname = x.FirstName,
        lastname = x.LastName,
        fullname = string.Format("{0}{1}", x.FirstName, x.LastName),
    });
}
数据库中不需要单独的继承实体类