【问题标题】:Allow nullable strings and List<string> as datatype in Entity Framework Model in ASP.NET MVC在 ASP.NET MVC 的实体框架模型中允许可空字符串和 List<string> 作为数据类型
【发布时间】:2013-12-21 16:33:24
【问题描述】:

我有一个模型类定义为:

public class EventReg
{
        public int ID { get; set; }

        [Display(Name = "Event Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EventDate { get; set; }

        [Display(Name = "Event Time")]
        [DataType(DataType.Time)]
        [DisplayFormat(DataFormatString = "{0:t}", ApplyFormatInEditMode = true)]
        public DateTime EventTime { get; set; }

        public List<string> HashTags { get; set; }
        public string Category { get; set; }

        [Display(Name = "Registered by")]
        public string UniqueId { get; set; }

        public float Latitude { get; set; }
        public float Longitude { get; set; }
        public string Description { get; set; }
}

问题:

  1. 现在我真的不知道每个事件是否会有 categoryHash Tags。所以,我想将这些字段注册为 Nullable 但是当我将这些字段定义为 System.Nullable 用于 categorySystem.Nullable > 对于HashTags,出现"The type string/List&lt;string&gt; must be non-nullable value type in order to use it as a parameter 'T' in the generic type or method'System.Nullable&lt;T&gt;'" 错误。如何解决这个错误?

  2. 我希望以后能够更新数据库架构,所以使用 包管理器控制台(添加迁移初始命令) 创建一个每次都会执行的 *datetime_initial.cs* 文件我调用更新数据库。但由于某种原因,字段 HashTags 未在创建的 datetime_initial.cs 文件中列出。为什么?

【问题讨论】:

  • 这里的全部情况是,字符串数据类型在设计上已经 IS 可以为空。所以你不需要在这里做任何事情(而且你也不允许这样做)。

标签: asp.net asp.net-mvc entity-framework


【解决方案1】:

试试这个:

public class EventReg
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] // !!!
    public int ID { get; set; }

    [Display(Name = "Event Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? EventDate { get; set; } // added "?"

    [Display(Name = "Event Time")]
    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString = "{0:t}", ApplyFormatInEditMode = true)]
    public DateTime? EventTime { get; set; } // added "?"

    public virtual ICollection<string> HashTags { get; set; } // changing List to ICollection virtual
    public string Category { get; set; }

    [Display(Name = "Registered by")]
    public string UniqueId { get; set; }

    public float Latitude { get; set; }
    public float Longitude { get; set; }
    public string Description { get; set; }
}

另外,随意去掉 EventDate 和 EventTime,因为 DateTime 保存日期和时间:

public DateTime? EventDateStamp { get; set; }

如果您需要时间或数据,您可以定义自己的只读属性。但不建议在模型中执行此操作,因为如果您保持模型一致,并且当您的模型不依赖于您将如何向用户展示它时,这被认为是一种好习惯。您可以为此目的使用视图模型并为此使用Automapper 工具。

另一个建议是摆脱 DateTime 并使用 DateTimeOffset:

public DateTimeOffset? EventDateStampUtc { get; set; }

具有通用日期和时间:

model.EventDateStampUtc = DateTimeOffset.Utc;

使用 UTC 时间的主要好处是,如果您有多个位于其他国家/地区、具有不同时间和时区的服务器,则无需考虑日期和时间。此外,您和您的客户不依赖于服务器的时区。

由于这个问题是关于 ASP.NET MVC 的,我可以建议您稍后在视图中显示此日期和时间。为此,我建议在客户端使用出色的 moment.js 库。

...每次调用 update-database 时都会执行...

为此目的在您的Migrations\Configuration.cs 中使用Seed 方法:

protected override void Seed(MyContext context)
{
    context.MyEntities.AddOrUpdate(
        e => e.Key,
        new MyEntity { aa = 1, bb = 2, key = "unique-001" },
        new MyEntity { aa = 11, bb = 22, key = "unique-002" }       
    );
}

配置构造函数中一些有用的选项:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true; // take care!
}

【讨论】:

  • @RomanPushkin...添加 ?将使其他字段可以为空,即它们可以接受 null 作为值。它是如何使 category 和 HashTags 可以为空的?另外,关于第二个问题的任何想法?
  • 我刚刚更新了第二个问题的答案。
  • 其实字符串是一种特殊的对象。您可以使用 String.IsNullOrEmpty 检查您的字符串是否为空。顺便说一句,你为什么希望你的字符串可以为空?
  • @Roman...这样,如果我们不知道字段的值或字段不适用(N/A),我们可以在数据库中插入 NULL 作为字段的内容
  • @Roman...为什么 HashTags 字段应该是虚拟的,ICollection 相对于 List 有什么好处
猜你喜欢
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 2010-11-20
  • 2014-01-18
  • 1970-01-01
相关资源
最近更新 更多