验证具有类似属性的实体框架6实体

时间:2014-04-06 11:39:13

标签: c# entity-framework entity-framework-6

我正在使用实体框架6(从我的数据库模型创建),并且有一个名为users的表,其中我有一些名为UserXml,UserExtranet,UserCustomer的相关表,所有这些表都具有相同的字段。

表格由外键链接。

根据用户类型,例如Xml / Extranet / Customer,我需要执行验证,例如启用UserXml,或者启用UserExtranet。

这是我目前所拥有的片段

//find the user
User oUser = Context.User.FirstOrDefault(u => u.Email == this.Email);

if (oUser != null)
{
   var oUserType = (dynamic)null;
   int iMaxAttempts;
   bool bValidIp = false; 

   //authenticate the type of user
   switch (this.Type)
   {
      case UserType.Xml:
          oUserType = oUser.UserXml; 
          //do the validation           
          break;

      case UserType.Api:
          oUserType = oUser.UserApi; 
          //do the validation           
          break;
      default:
          oUserType = null;
          break;
   }

}

这是我的实体。

用户类

public partial class User
{
    public User()
    {
    }

    public long UserId { get; set; }
    public string Email { get; set; }

    public virtual UserCustomer UserCustomer { get; set; }
    public virtual UserExtranet UserExtranet { get; set; }
    public virtual UserXml UserXml { get; set; }
}

客户类

public partial class UserCustomer
{
    public UserCustomer()
    {

    }

    public long UserId { get; set; }
    public long SiteId { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string Salt { get; set; }
    public byte FailedAttempts { get; set; }
    public System.DateTime LastLogin { get; set; }
    public bool Enabled { get; set; }
    public bool Deleted { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public System.DateTime LastChanged { get; set; }

    public virtual User User { get; set; }
}

UserExtranet类

public partial class UserExtranet
{
    public UserExtranet()
    {
    }

    public long UserId { get; set; }
    public long SiteId { get; set; }
    public string Password { get; set; }
    public string Salt { get; set; }
    public byte FailedAttempts { get; set; }
    public bool Enabled { get; set; }
    public bool Deleted { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public System.DateTime LastChanged { get; set; }

    public virtual User User { get; set; }
}

UserXml类

public partial class UserXml
{
    public UserXml()
    {
    }

    public long UserId { get; set; }
    public string Password { get; set; }
    public string Salt { get; set; }
    public byte FailedAttempts { get; set; }
    public bool Enabled { get; set; }
    public bool Deleted { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public System.DateTime LastChanged { get; set; }

    public virtual User User { get; set; }
}

如果不加倍每个表的代码,我怎样才能做到最好。

1 个答案:

答案 0 :(得分:1)

创建一个名为UserModel的第四个类并将各种User对象强制转换为它,然后验证UserModel。

public partial class UserModel
{
public UserModel()
{

}

public long UserId { get; set; }
public long SiteId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
public byte FailedAttempts { get; set; }
public System.DateTime LastLogin { get; set; }
public bool Enabled { get; set; }
public bool Deleted { get; set; }
public System.DateTime CreatedOn { get; set; }
public System.DateTime LastChanged { get; set; }

public virtual User User { get; set; }
}

然后

//find the user
User oUser = Context.User.FirstOrDefault(u => u.Email == this.Email);

if (oUser != null)
{
var oUserType = (dynamic)null;
int iMaxAttempts;
bool bValidIp = false; 

var ModelUser = new UserModel(){ UserId = oUser,UserId, etc.};

//authenticate the type of user
switch (this.Type)
{
   case UserType.Xml:
       oUserType = oUser.UserXml; 

      break;

  case UserType.Api:
      oUserType = oUser.UserApi; 

      break;
  default:
      oUserType = null;
      break;
}
ModelUser.Validate();
}