2015-10-13 76 views
1

我想将下面的类映射到一个表,使用实体框架,最好使用流利的API。实体框架序列化到一列的多个属性

public class MyEntity 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public int Age {get;set;} 
    public string OtherData {get;set;} 
    public List<Blabla> BlaBlaList {get;set;} 
} 

表myEntity所:

column Id 
column Name 
column SerializedData 

是否有可能只图Id和名称列,并且所有其他属性在 “SerializedData” 栏目连载?

如果没有“唯一”的其他属性,整个物体也可以在SerializedData列连载

感谢, 史蒂芬

+0

你想存储来自所有属性的数据合并和存储到一列? –

回答

3

类似德鲁的答案另一个答案会做这样的事情:

public class MyEntity : IMySerializable 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 

    [NotMapped] 
    public int Age {get;set;} 
    [NotMapped] 
    public string OtherData {get;set;} 
    [NotMapped] 
    public List<Blabla> BlaBlaList {get;set;} 

    public byte[] SerializedData 
    { 
    get 
    { 
     return this.MySerialize(); 
    } 
    set 
    { 
     this.MyDeserialize(value); 
    } 
    } 
} 

然后扩展方法允许你这样做是为了一个以上的实体:

public static IMySerializableExtensions 
{ 
    public static byte[] MySerialize<T>(this T instance) 
    where T : IMySerializable 
    { 
    byte[] result = // ... 

    // code 

    return result; 
    } 

    public static void MyDeserialize<T>(this T instance, byte[] value) 
    where T : IMySerializable 
    { 
    // deserialize value and update values 
    } 
} 

你自己看着办去掉哪些属性来反序列化/序列化,因为它们将会有NotMappedAttribute

3

你必须做你自己...

我建议为您的数据库映射创建一个单独的类,并将'MyEntity'作为POCO。这最终取决于偏好,但我更愿意让我的实体尽可能接近数据库结构。保持这种方式更容易。

所以说,创建一个单独的类,它是实际与之交互的对象,并为它提供一个实例方法来序列化自身,以及一个静态的反序列化方法。我在这里使用过JSON,但你可以做任何你想做的事情。注意我还添加了一个方法将其转换为MyDBEntity:您的逻辑可能会在其他地方,但这应该让您知道如何去做。

public class MyEntity 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public int Age { get; set; } 
    public string OtherData { get; set; } 
    public List<int> BlaBlaList { get; set; } 

    public byte[] Serialize() 
    { 
     string json = JsonConvert.SerializeObject(this); 
     return Encoding.ASCII.GetBytes(json); 
    } 

    public static string Deserialize(byte[] objectBytes) 
    { 
     return Encoding.ASCII.GetString(objectBytes); 
    } 

    public MyDBEntity ConvertToDBEntity() 
    { 
     MyDBEntity dbEntity = new MyDBEntity(); 
     dbEntity.ID = Id; 
     dbEntity.Name = Name; 
     dbEntity.SerializedData = this.Serialize(); 
     return dbEntity; 
    } 
} 

public class MyDBEntity 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public byte[] SerializedData { get; set; } 
} 

接下来,MyDBEntity类添加到您的上下文:

public class EFContext : DbContext 
{ 
    public DbSet<MyDBEntity> Entities { get; set; } 
} 

这就是它!现在你可以做的东西一样

using (var db = new EFContext()) 
    { 
     MyEntity me = new MyEntity(); 
     me.Name = "Bob"; 
     me.Age = 25; 
     me.OtherData = "he does stuff"; 
     me.BlaBlaList = new List<int> { 7, 8, 9 }; 
     MyDBEntity newEntity = me.ConvertToDBEntity(); 

     db.Entities.Add(newEntity); 
     db.SaveChanges(); 
    } 

我工作了一个小控制台应用程序用于这个答案,我把它放在Github如果你喜欢。