Error with entities that do not expose foreign key properties for their relationships
I've spent quiet some time building my Domain Model (using EF CTP5 Code First), and now I thought I'd through some test data and see if everything works. Unfortunately, it seems like my application is loaded with bugs that do not get caught except during runtime because everything compiles perfectly. Anyway, I keep getting the following error:
An error occurred while saving entities that do not expose foreign key properties for their relationships.
And Here's the inner exception:
"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."
I have no idea why this is happening. I suspected this might be caused by my initializer class in which I'm loading all the static content of the database inside the Seed() method. So I commented the part where I'm adding the ItemStatuses, and I still got the same error. Moreover, I should've gotten this on compile time, not during runtime, no?
Below is the ItemStatus class and part of my test data:
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: (requested by 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;
}
}
UPDATE 2:
public class UnitOfWork : IUnitOfWork
{
private readonly IDatabaseFactory _databaseFactory;
private DbContex开发者_高级运维t _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();
}
}
Any thoughts on the matter? Thanks in advance.
I think the error says that you don't have ItemStatus
record with Id = 1 in your database.
精彩评论