实体框架代码“首次日期”字段创建 - c#

我正在使用Entity Framework Code First方法创建数据库表。以下代码
在数据库中创建一个DATETIME列,但我想创建一个DATE列。

[DataType(DataType.Date)]
[DisplayFormatAttribute(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime ReportDate { get; set; }

在创建表期间,如何创建类型为DATE的列?

参考方案

大卫·罗斯的EF6版本的答案如下:

public class DataTypePropertyAttributeConvention 
    : PrimitivePropertyAttributeConfigurationConvention<DataTypeAttribute>
{
    public override void Apply(ConventionPrimitivePropertyConfiguration configuration, 
        DataTypeAttribute attribute)
    {
        if (attribute.DataType == DataType.Date)
        {
            configuration.HasColumnType("Date");
        }
    }
}

像以前一样注册:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     base.OnModelCreating(modelBuilder);

     modelBuilder.Conventions.Add(new DataTypePropertyAttributeConvention());
}

这与Tyler Durden的方法具有相同的结果,只是它使用EF基类进行工作。

Entity Framework DbEntityEntry>'不包含Where的定义 - c#

此代码狙击手来自Adding CreatedDate to an entity using Entity Framework 5 Code First public override int SaveChanges() { DateTime saveTime = DateTime.Now; foreach (var entry in this.ChangeT…

如何以编程方式将ListView滚动到最后一个元素-Compact Framework - c#

我正在使用Windows Mobile 6.1上的3.5 Compact Framework开发应用程序。我有一个ListView,添加项目时想自动滚动此列表。我能怎么做? 参考方案 listView.EnsureVisible(listView.Items.Count - 1);

模块化C#Compact Framework 2.0应用程序 - c#

我们目前正在开发新的手持软件。我无法讨论应用程序的性质,因此我将使用一个示例。我们正在设计用于管理学校的手持软件。我们希望对系统的各个方面进行模块化,以便不同的学校可以使用不同的功能。我们的系统将从主菜单和登录屏幕开始。我希望这可以作为系统的基础,并成为要添加模块的位置。即我将有一个名为SchoolPda的项目。然后,我想拥有不同的模块。即,我想要一个注册模…

在现有的SqlConnection中打开DbContext连接 - c#

我是否应该阻止在现有ADO.NET DbContext中打开实体框架的SqlConnection连接,前提是它们都使用相同的连接字符串,即在完全相同的数据库上运行?例如:using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new Syste…

如果Parameter为null,则排除条件 - c#

我正在使用LINQ和Lambda在2个条件下使用此查询来获取数据。是否可以在没有其他条件的情况下编写此逻辑-public List<Pallet> GetPallet(string palletID, string locationID) { List<Pallet> data = new List<Pallet>(); …