我花了一些時間來構建我的領域模型(使用EF CTP5代碼優先),現在我想我會通過一些測試數據,看看是否一切作品。不幸的是,似乎我的應用程序加載了一些錯誤,除了在運行時期之外,它們不會被捕獲,因爲一切都完美無缺。無論如何,我不斷收到以下錯誤:實體沒有爲其關係公開外鍵屬性的錯誤
An error occurred while saving entities that do not expose foreign key properties for their relationships.
,這裏是內部異常:
"The INSERT statement conflicted with the FOREIGN KEY constraint \"Item_ItemStatus\". The conflict occurred in database \"CFSharwe\", table \"dbo.ItemStatus\", column 'Id'.\r\nThe statement has been terminated."
我不知道爲什麼會這樣。我懷疑這可能是由我的initializer類導致的,我在其中加載了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();
UPDATE:(由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();
}
}
關於th的任何想法問題?提前致謝。
你在哪裏/如何管理你的DBContext? – 2011-03-23 18:24:42
您顯示了很多代碼,但沒有與您的問題相關的代碼。除了'ItemStatus'外,'Item'類和您創建Item和ItemStatus的代碼,將它們添加到DbContext並調用SaveChanges。此外,如果您將屬性放置在模型類屬性上,或者您使用Fluent API定製模型以及如何定製模型。並刪除這個Lore ipsum和圖像的東西... – Slauma 2011-03-23 18:27:30
@Derek Beattie:我正在使用UnitOfWork來管理DbConext。在上面的代碼段中(我編輯過),我調用categoryService的Save()方法,調用unitOfWork.Commit(),然後調用context.SaveChanges() – Kassem 2011-03-23 18:51:15