我想为每个用户存储一个设置对象(不想要设置表),但我认为我的方法非常错误。这就是我试图完成它的方式:
// this is the user model
public class AppUser: IdentityUser
{
public string FirstName {get; set;}
public string LastName {get; set;}
public UserSettings Settings {get; set;} // the desired object
}
// the settings class
[DataContract]
public class UserSettings
{
[DataMember]
public bool ANiceProperty {get; set;}
[DataMember]
public enum AnotherNiceEnumPropery {get; set;}
[DataMember]
public UserSettingsSubClass SubClass {get; set;}
// and so on...
}
// another sub-settings class
[DataContract]
public class UserSettingsSubClass
{
[DataMember]
public bool ANiceProperty {get; set;}
}
使用它的最佳方式是什么,例如User.Settings.Property?在我疯狂的PHP和MySQL时代,我要创建一个数组 - 序列化它 - 存储为字符串 - 反序列化 - 返回数组。但我认为还有更好的方法,不是吗?
答案 0 :(得分:1)
无论如何,您必须将设置序列化为字符串。我建议你使用Json.Net library
使用示例:
Product product = new Product();
product.Name = "Apple";
product.ExpiryDate = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JsonConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "ExpiryDate": "2008-12-28T00:00:00",
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);