c# - How can I map an array property to a delimited string db field using EF? -


i have object array property want persist in database delimited string. how map property field in database , vice versus?

public class user() {   public int id { get; set; }   public string[] roles { get; set; } } 

incomplete config class:

public class userconfig : entitytypeconfiguration<user> {   public userconfig() {     this.property(u => u.roles).__???__     this.map(u => u.properties<string[]>(r => r.roles).__???__))       .hascolumnname("roles");   } } 

for example "roles" property converted "rolea,roleb,rolec" when going database , transformed array when read database. there on data mapping event somewhere?

you need additional property wraps , converts string string[].

public class user() {   public int id { get; set; }   public string roles { get; set; }   public string[] rolesarray    {          {       return roles.split(',').toarray();     }     set     {       roles = string.join(',', value);     }   } } 

the preferred solution of course add new table database called role, , have one-many relationship user has many roles. allow ef manage you, , means data stored coherently , accessibly. comma delimited strings not particularly pleasant work , shouldn't stored in databases.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -