C#和EF实体赢得';t添加为保存到实体集
我的应用程序中有EF是用C编写的,我使用了C#和EF实体赢得';t添加为保存到实体集,c#,entity-framework,C#,Entity Framework,我的应用程序中有EF是用C编写的,我使用了.edmx文件 我有两个收藏: public virtual DbSet<Annotation> Annotations { get; set; } public virtual DbSet<ProductionExclusions> ProductionExclusions { get; set; } 公共虚拟数据库集 在ProductionExclusions的项中,我没有此属性。这两个实体都使用相同的代码来创建实体。为
.edmx
文件
我有两个收藏:
public virtual DbSet<Annotation> Annotations { get; set; }
public virtual DbSet<ProductionExclusions> ProductionExclusions { get; set; }
公共虚拟数据库集
在ProductionExclusions的项中,我没有此属性。这两个实体都使用相同的代码来创建实体。为了调试和修复此问题,有几点需要验证
验证数据库和edmx中的两个位置是否正确添加和更新了所有字段
检查类型,尽管根据代码它似乎是正确的。但需要一步来验证
尝试向对象提供硬编码值,然后重试
最后放下你的.edmx并重新生成它
为日期字段设置一些仅用于测试的默认值
请尝试一下,您可能会从中得到一些帮助。我看不到您正在保存对数据库的更改,为什么希望通过查询获取您的实体?它是作为临时实体创建的:entry.State=System.Data.entity.EntityState.Added代码>和应显示在model.ProductionExclusions中,并已设置标志IsTempSaved=true
,但从代码中看不到您试图使用ProductionExclusions.ToArray()
从数据库获取实体,但数据库中还没有这样的实体。您必须先执行SaveChanges
。查询不会返回任何临时实体是的,你说得对,我想在网格中显示临时添加的实体,我必须创建本地集合,和从数据库添加项目,然后添加临时创建的项目并在gridI中显示此集合从edmx中删除的实体,并再次从数据库和向导中选择ProductionExclusions进行更新,但仍然无法添加项目。我使用INSERT one项添加到数据库中,我可以在代码中获取它,但Create仍然不起作用
public partial class Annotation: RowBase, IRow
{
Dictionary<string, object> tempData;
public void SaveTemp()
{
if(tempData == null)
tempData = new Dictionary<string, object>();
tempData["Type"] = Type;
tempData["StartingDate"] = StartingDate;
tempData["EndingDate"] = EndingDate;
tempData["Description"] = Description;
}
}
public partial class ProductionExclusions: RowBase, IRow
{
Dictionary<string, object> tempData;
public void SaveTemp()
{
if(tempData == null)
tempData = new Dictionary<string, object>();
tempData["StartingDate"] = StartingDate;
tempData["EndingDate"] = EndingDate;
}
}
var item = model.Set<ProductionExclusions>().Create();
DateTime date = DateTime.Today;
item.StartingDate = date;
item.EndingDate = date;
var entry = model.Entry(item);
if (entry.State == System.Data.Entity.EntityState.Detached)
entry.State = System.Data.Entity.EntityState.Added;
item.SaveTemp();
var productionExclusions = from e in model.ProductionExclusions
orderby e.StartingDate
select e;
var result = productionExclusions.ToArray();