小编Dan*_*ann的帖子

实体框架 - 一般地保持枚举?

尝试使用Generics创建在Entity Framework中处理Enums的变通方法,但EF似乎并不关心通用属性.例如:

public enum MyEnum
{
    One, Two, Three
}

public class SomePOCOWithEnum
{
      // I want this to persist as an int, but EF doesn't like generics.
      public EnumWrapper<MyEnum> MyEnumProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

目的是使枚举在数据库中作为INT持久存在.是否有任何特殊的方法使用流利或者其他一些机制方法,我可以创建所述泛型类并将其作为INT持久存储到EF中的数据库中?

目的是保持通用性,因为我有大约24个需要持久化的枚举,而我宁愿不为每个枚举类编写单独的包装类.

这是通用的EnumWrapper类,它演示了我想要实现的内容:隐式转换为枚举,但持久性为int:

public class EnumWrapper<T> where T : struct
{
    private T enumValue;
    public int Value
    {
        get { return Convert.ToInt32(enumValue); }
        set { enumValue = (T)Enum.Parse(typeof(T), value.ToString()); }
    }

    public T EnumValue
    {
        get { return enumValue; }
        set { enumValue = …
Run Code Online (Sandbox Code Playgroud)

c# generics enums frameworks entity

6
推荐指数
1
解决办法
831
查看次数

C# - 快速分配所有字段/属性的片段或模板?

对于可能知道答案的人的快速点 - 是否有一个片段或工具可以快速生成模板代码以分配对象的所有公共字段和/或属性?

例:

public class SomeBloatedClass
{
    public string SomeField1 { get; set; }
    public int SomeField2 { get; set; }
    // etc...
    public string SomeField99 { get; set; }
}

public class TestHarness
{
    public SomeBloatedClass CreateTestObject()
    {
         // Is there a snippet/macro/template that can generate the code to assign
         // all public fields/properties so they can be manually assigned quickly?
         // Something like this...?

         // *Begin auto-generated code
         SomeBloatedClass s = new SomeBloatedClass();
         s.SomeField1 = ;
         s.SomeField2 …
Run Code Online (Sandbox Code Playgroud)

c# resharper visual-studio code-snippets

6
推荐指数
1
解决办法
1331
查看次数