EF代码首先List不创建表

发布于 2024-12-19 00:53:12 字数 711 浏览 3 评论 0原文

我正在使用一个实体创建一个非常简单的 EF4 代码优先项目。

public class Activity
{
    public Guid Id { get; set; }
    public string Description { get; set; }
    public List<DateTime> DateDone { get; set; }
    <snip>

    public Activity()
    {
        if(DateDone==null)
            DateDone = new List<DateTime>();
    }
}

我重建我的项目,然后运行 ​​MVC3 应用程序,数据库生成(已确认,我已添加和删除列),我的数据已播种(当我修改播种数据时,它在屏幕上发生变化)

我能够:

var activity = db.Activities.Find(id);
activity.DateDone = DateTime.Now;
db.SaveChanges();

但数据不是没有保存。我已经检查了数据库,因为只有一个表(活动)并且它具有所有适当的字段。我希望它应该有第二个表,ActivityDateDone,有两个字段,ActivityGuid 和 ActivityDateDone。日期完成。

我在完成这项工作时缺少什么?

I am creating a very simple EF4 code first project with one entity.

public class Activity
{
    public Guid Id { get; set; }
    public string Description { get; set; }
    public List<DateTime> DateDone { get; set; }
    <snip>

    public Activity()
    {
        if(DateDone==null)
            DateDone = new List<DateTime>();
    }
}

I rebuild my project and then run the MVC3 app and the database generates (confirmed, I have added and removed columns), my data is seeded (it changes on screen when I modify the seeded data)

I am able to:

var activity = db.Activities.Find(id);
activity.DateDone = DateTime.Now;
db.SaveChanges();

But the data isn't saved. I have checked the database as there is only the one table (Activity) and it has all the appropriate fields. I expect it should have a second table though, ActivityDateDone with two fields, ActivityGuid & DateDone.

What am I missing on making this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2024-12-26 00:53:12

实体框架不支持原始类型的集合。

您需要定义一个单独的类来保存日期时间。

Entity Framework does not support collections of primitive types.

You need to define a separate class to hold the DateTimes.

|煩躁 2024-12-26 00:53:12

DateTime 不是一个实体。如果您希望首先使用代码来创建数据结构,则需要创建一个模型类。我假设您的活动表有一个 DateDone 列?

DateTime is not an entity. You need to create a model class if you want code first to create data structures for you. I assume your activity table has a DateDone Column?

挽容 2024-12-26 00:53:12

虽然实体框架本身不支持此功能,但您可以让它很好地工作。

请参阅此答案以获取附加代码(PersistableScalarCollection)和原始想法:https://stackoverflow.com/a/11990836/1742393

/// <summary>
///     ALlows persisting of a simple DateTime collection.
/// </summary>
[ComplexType]
public class PersistableDateTimeCollection : PersistableScalarCollection<DateTime>
{
    protected override DateTime ConvertSingleValueToRuntime(string rawValue)
    {
        return DateTime.Parse(rawValue);
    }

    protected override string ConvertSingleValueToPersistable(DateTime value)
    {
        return value.ToShortDateString();
    }
}

While Entity Framework does not support this natively, you can make it work quite nicely.

Please see this answer for the additional code(PersistableScalarCollection) and original idea: https://stackoverflow.com/a/11990836/1742393

/// <summary>
///     ALlows persisting of a simple DateTime collection.
/// </summary>
[ComplexType]
public class PersistableDateTimeCollection : PersistableScalarCollection<DateTime>
{
    protected override DateTime ConvertSingleValueToRuntime(string rawValue)
    {
        return DateTime.Parse(rawValue);
    }

    protected override string ConvertSingleValueToPersistable(DateTime value)
    {
        return value.ToShortDateString();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文