Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance 实体框架4.1,表/类型继承_Inheritance_Code First_Table Per Type - Fatal编程技术网

Inheritance 实体框架4.1,表/类型继承

Inheritance 实体框架4.1,表/类型继承,inheritance,code-first,table-per-type,Inheritance,Code First,Table Per Type,有人能提出一个C#限制的解决方案吗?我需要两个不同的类型都从相同的基类型派生。在我的示例中,员工和用户都是人。员工也可以是用户(反之亦然)。这似乎是多重继承,这在C#中是不受支持的。我已经复习过了,这似乎不符合我的设想 public abstract class Person { public int ID { get; set; } public string Name { get; set; } public string Email { get; set;

有人能提出一个C#限制的解决方案吗?我需要两个不同的类型都从相同的基类型派生。在我的示例中,员工和用户都是人。员工也可以是用户(反之亦然)。这似乎是多重继承,这在C#中是不受支持的。我已经复习过了,这似乎不符合我的设想

public abstract class Person
  {

    public int ID { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }
  }

  public class Employee : Person
  {

    public string Number { get; set; }

    public System.DateTime HireDate { get; set; }
  }

  public class User : Person
  {

    public string Password { get; set; }

    public bool Active { get; set; }
  }

  public class CfTestContext : DbContext
  {

    public DbSet<Employee> Employees { get; set; }

    public DbSet<User> Users { get; set; }

    public DbSet<Person> People { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

      modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

      modelBuilder.Entity<Person>().ToTable("People");

      modelBuilder.Entity<Employee>().ToTable("Employees");

      modelBuilder.Entity<User>().ToTable("Users");
    }
  }

  public class CreateCfTestDatabase : DropCreateDatabaseAlways<CfTestContext>
  {

    protected override void Seed(CfTestContext context)
    {

      // Abraham Lincoln hired as an Employee on February 12th.

      // Abraham doesn't need access as a User because he doesn't use the Purchasing application.

      context.Employees.Add(new Employee { Name = "Abraham Lincoln", Email = "abraham@microsoft.com", Number = "107124", HireDate = System.DateTime.Parse("2/12/2000") });

      // George Washington added as a User on July 4th.

      // George is a consultant, so he will never become an Employee.

      context.Users.Add(new User { Name = "George Washington", Email = "george@microsoft.com", Password = "xT76#a2", Active = true });

      context.SaveChanges();

      // Make Abraham Lincoln a User on September 29th.

      // Abraham received training and now needs to use the Purchasing application

      Employee employee = context.Employees.Where(t => t.Name == "Abraham Lincoln").FirstOrDefault();

      context.Users.Add(new User { Password = "C1x$av38", Active = true,  ID = employee.ID });  // this does not produce the desired results.

      context.SaveChanges();

      base.Seed(context);
    }
  }
公共抽象类人物
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共字符串电子邮件{get;set;}
}
公共类员工:人
{
公共字符串编号{get;set;}
public System.DateTime HireDate{get;set;}
}
公共类用户:Person
{
公共字符串密码{get;set;}
公共bool活动{get;set;}
}
公共类CfTestContext:DbContext
{
公共数据库集雇员{get;set;}
公共数据库集用户{get;set;}
公共数据库集人物{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
modelBuilder.Entity().ToTable(“人”);
modelBuilder.Entity().ToTable(“员工”);
modelBuilder.Entity().ToTable(“用户”);
}
}
公共类CreateCftTestDatabase:DropCreateDatabaseAlways
{
受保护的覆盖无效种子(CfTestContext上下文)
{
//亚伯拉罕·林肯于2月12日被聘为雇员。
//Abraham不需要作为用户访问,因为他不使用购买应用程序。
Add(新员工{Name=“Abraham Lincoln”,Email=”)abraham@microsoft.com,Number=“107124”,HireDate=System.DateTime.Parse(“2/12/2000”)};
//乔治·华盛顿于7月4日以用户身份加入。
//乔治是一名顾问,所以他永远不会成为一名雇员。
添加(新用户{Name=“George Washington”,电子邮件=”george@microsoft.com,Password=“xT76#a2”,Active=true});
SaveChanges();
//让亚伯拉罕·林肯在9月29日成为用户。
//Abraham接受了培训,现在需要使用采购应用程序
Employee=context.Employees.Where(t=>t.Name==“Abraham Lincoln”).FirstOrDefault();
context.Users.Add(新用户{Password=“C1x$av38”,Active=true,ID=employee.ID});//这不会产生所需的结果。
SaveChanges();
种子(上下文);
}
}

没错,在C#中不可能创建多重继承对象。但是,您可以通过在模型中创建ComplexType(例如Login)并添加密码和活动属性(将登录属性添加到person对象)来解决此问题。然后,您可以通过检查Person对象上的login.active来检查用户是否具有活动的登录信息。

您是对的,在C#中不可能创建多继承对象。但是,您可以通过在模型中创建ComplexType(例如Login)并添加密码和活动属性(将登录属性添加到person对象)来解决此问题。然后,您可以通过检查Person对象上的login.active来检查用户是否有活动的登录信息。

感谢Espen及时的回答。感谢Espen及时的回答。