我正在使用角色访问基础控制项目,我需要在每个角色的Mysql数据库中存储一个列表。这是我的代码......
public class LoginModel
{
[Key]
public int lm_Id { get; set; }
public string User { get; set; }
public string Pass { get; set; }
public virtual ICollection<Roles> roles { get; set; }
public LoginModel()
{
roles = new HashSet<Roles>();
}
}
public class Roles
{
[Key]
public int role_Id { get; set; }
public string Name { get; set; }
public virtual ICollection<LoginModel> Users { get; set; }
public virtual List<string> Permissions { get; set; }
public Roles(){
Users = new List<LoginModel>();
}
}
我希望存储公共虚拟List<string> Permissions { get; set; }
的每个角色的权限列表,但是当我尝试在我的角色中插入一些记录时,表中没有字符串列表的列{{1} }
List<string> Permissions { get; set; }
以下是插入后MySql数据库角色表显示的内容:
我正在插入的using (var db = new MySqlContext())
{
#region insert
var l = new List<string>() { "Permission1", "Permission2", "Permission3" };
db.LoginModel.Add(
new LoginModel
{
User = "rafa",
Pass = "123",
roles =
{
new Roles {Name = "usuario", Permissions = l},
new Roles {Name = "usuario2", Permissions = l}
}
}
);
db.LoginModel.Add(
new LoginModel
{
User = "Ralph",
Pass = "123",
roles =
{
new Roles {Name = "Admin", Permissions = l},
new Roles {Name = "Admin2", Permissions = l}
}
}
);
db.SaveChanges();
#endregion
}
不在表格中。
我正在使用Entity Framework 5 + MySql db + C#
希望玩具人可以帮助我!答案 0 :(得分:1)
您可以将角色存储在私有属性中,作为单个逗号分隔的字符串:
并从RolesInternal中读取Roles属性:
private string RolesInternal { get; set; }
[NotMapped]
public List<string> Roles
{
get { return RolesInternal.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(t => t).ToList(); }
set { RolesInternal = string.Join(",", value); }
}
有关映射私有属性的示例代码:
http://romiller.com/2012/10/01/mapping-to-private-properties-with-code-first/