我花了一些时间来构建我的域模型(使用EF CTP5 Code First),现在我想我会通过一些测试数据看看是否一切正常。不幸的是,似乎我的应用程序加载了除了在运行时期间没有被捕获的错误,因为一切都完美编译。无论如何,我一直收到以下错误:
保存时发生错误 不暴露外国的实体 他们的关键属性 关系。
这是内在的例外:
“INSERT语句与...发生冲突 FOREIGN KEY约束 \ “Item_ItemStatus \”。冲突 发生在数据库“CFSharwe”中, table \“dbo.ItemStatus \”,列 'Id'。\ r \ n声明已经发布 终止“。
我不知道为什么会这样。我怀疑这可能是由我的初始化类引起的,我在其中加载了Seed()方法中数据库的所有静态内容。所以我评论了我添加ItemStatuses的部分,我仍然遇到了同样的错误。而且,我应该在编译时得到这个,而不是在运行时,不是吗?
下面是ItemStatus类和我测试数据的一部分:
public class ItemStatus
{
public int Id { get; set; }
public string Description { get; set; }
public ICollection<Item> Items { get; set; }
}
//Test data inside the Index() method of the ItemsController
var item2 = new Item
{
Title = "iPhone 4",
Description = "Lorem Ipsum is simply",
StartingPrice = 400f,
User = user2,
Status = 1,
EndDate = DateTime.Now.AddDays(10),
StartDate = DateTime.Now,
BidIncrement = 3f,
Bids = new List<Bid>(),
Comments = new List<Comment>(),
//ItemStatus = _itemsService.GetItemStatusById(1),
ViewingUsers = new List<User>(),
WatchingUsers = new List<User>(),
Tags = new List<Tag>()
};
//here's where I save the data to the database
var category = _categoryService.GetChildByName(categoryName);
category.Items.Add(item2);
_categoryService.Save();
更新:(Slauma请求)
public class Item
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public float StartingPrice { get; set; }
public float? BidIncrement { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int Status { get; set; }
[ForeignKey("Status")]
public virtual ItemStatus ItemStatus { get; set; }
public virtual Address PickupAddress { get; set; }
public virtual User User { get; set; }
public virtual ChildCategory Category { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Image> Images { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<User> WatchingUsers { get; set; }
public virtual ICollection<User> ViewingUsers { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
//public virtual ItemRating Rating { get; set; }
public bool IsValidBidAmount(int amount)
{
if (amount <= this.Bids.Max(a => a.Amount))
return false;
return true;
}
public bool IsClosed()
{
if (this.ItemStatus.Id.Equals(3))
return true;
return false;
}
public bool IsPending()
{
if (ItemStatus.Id.Equals(2))
return true;
return false;
}
public bool IsPublished()
{
if (ItemStatus.Equals(1))
return true;
return false;
}
public int WinnerId()
{
if(IsClosed())
{
User highestBidder = null;
foreach (Bid b in Bids)
{
if (b.Amount.Equals(HighestBid()))
highestBidder = b.User;
}
if (highestBidder != null) return highestBidder.Id;
}
return 0;
}
public float HighestBid()
{
return Bids.Max(u => u.Amount);
}
public string MainImageLink()
{
var mainImage = Images.Single(i => i.Rank.Equals(0));
return mainImage.Path;
}
public string FirstTag()
{
return Tags.First().Title;
}
}
更新2:
public class UnitOfWork : IUnitOfWork
{
private readonly IDatabaseFactory _databaseFactory;
private DbContext _context;
public UnitOfWork(IDatabaseFactory dbFactory)
{
_databaseFactory = dbFactory;
}
protected DbContext DataContext
{
get
{
return _context ?? (_context = _databaseFactory.GetDbContext());
}
}
public void Commit()
{
DataContext.SaveChanges();
}
}
public class EfDatabaseFactory : IDisposable, IDatabaseFactory
{
private SharweEntities _dbContext;
public DbContext GetDbContext()
{
return _dbContext ?? (_dbContext = new SharweEntities());
}
public System.Data.Objects.ObjectContext GetObjectContext()
{
return _dbContext.ObjectContext;
}
public void Dispose()
{
if (_dbContext != null)
_dbContext.Dispose();
}
}
有关此事的任何想法?提前谢谢。
答案 0 :(得分:1)
我认为错误表明您的数据库中没有Id = 1的ItemStatus
记录。