2011-12-24 14 views
1

誰かがC#の制限の回避策を提案できますか?私は同じ基本型から派生する2つの異なる型が必要です。私の例では、従業員とユーザーは人です。従業員もユーザーになることができます(またはその逆)。これはC#ではサポートされていない多重継承のようです。私はhttp://www.codeproject.com/KB/architecture/smip.aspxを見直しましたが、私のシナリオに合わないようです。Entity Framework 4.1、種類ごとの継承テーブル

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 = "[email protected]", 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 = "[email protected]", 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); 
    } 
    } 

答えて

1

C#で複数の継承オブジェクトを作成する可能性はありません。しかし、この問題は、モデル(例:ログイン)にComplexTypeを作成し、パスワードとアクティブプロパティを追加し、PersonオブジェクトにLoginプロパティを追加することで解決できます。その後、PersonオブジェクトのLogin.Activeをチェックすることで、ユーザーにアクティブなログイン情報があるかどうかを確認できます。

+0

タイムリーな回答をいただきありがとうございます。 – tebo

関連する問題