我有群组用户类:
[Table("tblGroupUser")]
public class UserGroup : MyTemplate
{
public UserGroup()
{
Users = new List<User>();
}
[Key]
[DisplayName("Group Code"), Column("GroupCode", TypeName = "varchar")]
[MaxLength(20, ErrorMessage = "Group Code maximum length is {0}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "{0} is required")]
public string GroupCode { get; set; }
[DisplayName("Group Name")]
[MaxLength(20, ErrorMessage = "Group Code maximum length is {0}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "{0} is required")]
public string GroupName { get; set; }
public virtual List<User> Users { get; set; }
}
用户类:
[Table("tblUser")]
public class User : MyTemplate
{
public User()
{
UserGroup = new UserGroup();
}
[Key]
[Required(AllowEmptyStrings = false, ErrorMessage = "User ID is required and use for login.")]
[DisplayName("User ID"), Column("UserID", TypeName = "varchar")]
[MaxLength(15, ErrorMessage = "User ID maximum length is 15 characters.")]
public string UserID { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "User name is required")]
[MaxLength(20, ErrorMessage = "User name maximum length is 20 charachters")]
[DisplayName("User Name"), Column("UserName", TypeName = "varchar")]
public string UserName { get; set; }
[DisplayName("Email"), Column("Email", TypeName = "varchar")]
[DataType(DataType.EmailAddress)]
[MaxLength(128, ErrorMessage = "Email address maximum length is 128 charachters")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Email address is required.")]
public string Email { get; set; }
[DisplayName("Password"), Column("Password", TypeName = "varchar")]
[DataType(DataType.Password)]
[Required(AllowEmptyStrings = false, ErrorMessage = "Password is required.")]
[MaxLength(128)]
[MinLength(6, ErrorMessage = ("Password at least 6 charachters."))]
public string Password { get; set; }
[DisplayName("User Group"), Column("GroupCode", TypeName="varchar")]
[Required(AllowEmptyStrings = false, ErrorMessage = "User group is required")]
[MaxLength(20, ErrorMessage = "Group code maximum length is 20 charachters")]
[ForeignKey("UserGroup")]
public string GroupCode { get; set; }
[DisplayName("Status"), Column("Status", TypeName = "tinyint")]
[Required(AllowEmptyStrings = false, ErrorMessage = "User status is required")]
public UserStatus Status { get; set; }
[DisplayName("Roles"), Column("Roles", TypeName = "varchar")]
[MaxLength(32)]
[Required(AllowEmptyStrings = false, ErrorMessage = "Roles is required.")]
public string Roles { get; set; }
public virtual UserGroup UserGroup { get; set; }
}
这是我的用户背景:
public class UserDBContext : DbContext
{
public UserDBContext() : base("DConn")
{
Database.SetInitializer<UserDBContext>(new CreateDatabaseIfNotExists<UserDBContext>());
}
public DbSet<User> Users { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
}
如果我在GroupCode或UserGroup属性中添加外键,则不会创建数据库表(tblUser)。我搜索了许多解决方案而没有运气。我尝试使用流畅的API并遇到同样的问题。有人可以帮帮我吗?