EF代码首先List不创建表
我正在使用一个实体创建一个非常简单的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实体框架不支持原始类型的集合。
您需要定义一个单独的类来保存日期时间。
Entity Framework does not support collections of primitive types.
You need to define a separate class to hold the DateTimes.
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?
虽然实体框架本身不支持此功能,但您可以让它很好地工作。
请参阅此答案以获取附加代码(PersistableScalarCollection)和原始想法:https://stackoverflow.com/a/11990836/1742393
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