[英]Validating Entity Framework 6 entities that have similar properties
I'm using entity framework 6 (created from my database model), and have a table called users, of which I have some related tables called UserXml, UserExtranet, UserCustomer, all of these tables have the same fields. 我使用的是实体框架6(从我的数据库模型创建),并且有一个名为users的表,其中有一些名为UserXml,UserExtranet,UserCustomer的相关表,所有这些表都具有相同的字段。
The tables are linked by foreign keys. 这些表通过外键链接。
Based on the user type eg Xml/Extranet/Customer, I need to perform validation like is the UserXml enabled, or is the UserExtranet enabled. 根据用户类型(例如Xml / Extranet / Customer),我需要执行验证,例如启用了UserXml还是启用了UserExtranet。
Here is a snippet of what I have at the moment 这是我目前所拥有的片段
//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;
}
} }
Here are my entities. 这是我的实体。
The user class 用户类别
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; }
}
The customer class 客户阶层
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; }
}
The UserExtranet class 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; }
}
The UserXml class 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; }
}
How best can I achieve this without doubling up the code for each table. 我如何最好地做到这一点而又不增加每个表的代码。
Create a fourth class called UserModel and cast your various User objects to it, then validate the UserModel. 创建一个名为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; }
}
And then 接着
//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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.