我在实体“组合”中添加了一些字典。其中的值不会由用户直接更新,而是实体本身具有填充/刷新此字典的方法。
我正在从另一个控制器调用此刷新方法,就在返回操作之前:
..
portfolio.refreshDicts();
db.SaveChanges();
通过调试,我可以看到 refreshDicts() 向 dict 添加了 6 个值,就像它应该做的那样。但是这些并没有被存储,尽管调用 db.SaveChanges() ...当我尝试访问另一个控制器中的 dict 时,我可以看到它是空的。
在尝试调用 refreshDicts() (将对象添加到投资组合中的列表)之前,我正在同一个 Controller 方法中对投资组合执行另一项操作,并且已正确保存。
怎么可能?感谢您的帮助!
更新
带有字典的班级组合
public class Portfolio
{
[Key]
public int ID { get; set; }
public string GroupID { get; set; }
public virtual List<KassenwirksamesEreignis> KassenwirksameEreignisse { get; set; }
public virtual List<InternMsg> InterneNachrichten { get; set; }
public virtual List<StockPosition> StockPositions { get; set; }
public virtual List<BondPosition> BondPositions { get; set; }
public virtual List<FuturePosition> FuturePositions { get; set; }
public virtual List<OptionPosition> OptionPositions { get; set; }
public virtual List<FondsPosition> FondsPositions { get; set; }
public virtual List<Regelereignis> RegelEreignisse { get; set; }
public Dictionary<DateTime, double> FondsVolumenDict { get; set; }
public Dictionary<DateTime, double> KasseDict { get; set; }
public Dictionary<DateTime, double> StockDict { get; set; }
public Dictionary<DateTime, double> OptionDict { get; set; }
public Dictionary<DateTime, double> FutureDict { get; set; }
public Dictionary<DateTime, double> FondsDict { get; set; }
public Dictionary<DateTime, double> BondDict { get; set; }
public Portfolio()
{
FondsVolumenDict = new Dictionary<DateTime, double>();
KasseDict = new Dictionary<DateTime, double>();
StockDict = new Dictionary<DateTime, double>();
OptionDict = new Dictionary<DateTime, double>();
FutureDict = new Dictionary<DateTime, double>();
FondsDict = new Dictionary<DateTime, double>();
BondDict = new Dictionary<DateTime, double>();
}
控制器:
public ActionResult Create(StockPosition position)
{
if (ModelState.IsValid)
{
// find user's portfolio
string currentUser = User.Identity.Name;
string userGroupId = db.UserProfiles.Single(x => x.UserName == currentUser).GroupId;
Portfolio portfolio = db.Portfolios.Single(x => x.GroupID == userGroupId);
// add Stock to portfolio
position.PortfolioID = portfolio.ID;
db.StockPositions.Add(position);
// book costs
var ke = new KassenwirksamesEreignis();
ke.Art = "Kauf";
ke.Bezeichnung = position.Bezeichnung;
ke.Date = DateTime.Now;
ke.Wert = Math.Round((-1.0) * (position.Menge * position.Kaufpreis), 2);
portfolio.KassenwirksameEreignisse.Add(ke);
// Dictionaries refreshen
portfolio.refreshDicts();
db.Entry(portfolio).State = EntityState.Modified;
// Save & Back
db.SaveChanges();
return RedirectToAction("../Portfolio/Index");
}
return RedirectToAction("../Portfolio/Index");
}
我的背景:
public class PlanspielContext : DbContext
{
public DbSet<Portfolio> Portfolios { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<GlobalMsg> GlobalNachrichten { get; set; }
public DbSet<InternMsg> InterneNachrichten { get; set; }
public DbSet<Price> Prices { get; set; }
public DbSet<KassenwirksamesEreignis> KassenwirksameEreignisse { get; set; }
public DbSet<Zinsanspruch> Zinsansprüche { get; set; }
public DbSet<StockPosition> StockPositions { get; set; }
public DbSet<OptionPosition> OptionPositions { get; set; }
public DbSet<FuturePosition> FuturePositions { get; set; }
public DbSet<BondPosition> BondPositions { get; set; }
public DbSet<FondsPosition> FondsPositions { get; set; }
public DbSet<Regelereignis> Regelereignisse { get; set; }
// Am not sure if i need those here .. ?
//public Dictionary<DateTime, double> FondsVolumenDict { get; set; }
//public Dictionary<DateTime, double> KasseDict { get; set; }
//public Dictionary<DateTime, double> StockDict { get; set; }
//public Dictionary<DateTime, double> OptionDict { get; set; }
//public Dictionary<DateTime, double> FutureDict { get; set; }
//public Dictionary<DateTime, double> FondsDict { get; set; }
//public Dictionary<DateTime, double> BondDict { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
我正在调用的 refreshDict 方法
public void refreshDicts()
{
refreshBondDict();
refreshFondsDict();
refreshFutureDict();
refreshOptionDict();
refreshStockDict();
refreshKasseDict();
refreshFondsVolumenDict();
}
public void refreshFondsDict()
{
DateTime today = DateTime.Now;
for ( var i = - 4; i <= 1; i++)
{
DateTime currentDay = today.AddDays(i).Date;
if (this.FondsDict.ContainsKey(currentDay))
{
this.FondsDict[currentDay] = getFondsValue(currentDay);
}
else
{
this.FondsDict.Add(currentDay, getFondsValue(currentDay));
}
}
}
....