I have a database and in that I have a table named "GuestBook". Now when I try to get the data out of there I get this error:
Invalid object name 'dbo.GuestBooks'.Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.GuestBooks'. Source Error:
Line 45: else
Line 46: {
Line 47: return query.ToList(); Line 48: }
Line 49: }
The problem is that your table is named GuestBook, but your DbSet in your context is named GuestBooks. By default these names have to match, that's how EF code first figures out which table to go to. However, since the names don't match you get an error.
You could rename GuestBooks in the context to GuestBook.
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<GuestBook> GuestBook { get; set; }
But then you have to change all references in your code to GuestBooks.
Another option is to explicitly tell EF code first that the table for GuestBooks is guestbook. You would do this by adding an annotation to the GuestBook object like so:
[Table("GuestBook")]
public class GuestBook{
// your class details
}
svetsarn
Member
487 Points
612 Posts
Pluralize?
Dec 30, 2012 03:44 PM|LINK
I have a database and in that I have a table named "GuestBook". Now when I try to get the data out of there I get this error:
Invalid object name 'dbo.GuestBooks'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.GuestBooks'.
Source Error:
Line 45: else Line 46: { Line 47: return query.ToList(); Line 48: } Line 49: }Source File: ...........\Dal\GenericRepository.cs Line: 47
GenericRepository
public virtual IEnumerable<TEntity> Get( Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "") { IQueryable<TEntity> query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } }UserContext
public UsersContext() : base("DefaultConnection") { } public DbSet<UserProfile> UserProfiles { get; set; } public DbSet<GuestBook> GuestBooks { get; set; }UnitOfWork
private UsersContext context = new UsersContext(); private GenericRepository<GuestBook> guestBookRepository; public GenericRepository<GuestBook> GuestBookRepository { get { if (this.guestBookRepository == null) { this.guestBookRepository = new GenericRepository<GuestBook>(context); } return guestBookRepository; } }I knpw there is somethong about plurelize questions, but I cant fins a solution for this.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Pluralize?
Dec 30, 2012 04:33 PM|LINK
The problem is that your table is named GuestBook, but your DbSet in your context is named GuestBooks. By default these names have to match, that's how EF code first figures out which table to go to. However, since the names don't match you get an error. You could rename GuestBooks in the context to GuestBook.
public UsersContext() : base("DefaultConnection") { } public DbSet<UserProfile> UserProfiles { get; set; } public DbSet<GuestBook> GuestBook { get; set; }But then you have to change all references in your code to GuestBooks.
Another option is to explicitly tell EF code first that the table for GuestBooks is guestbook. You would do this by adding an annotation to the GuestBook object like so:
[Table("GuestBook")] public class GuestBook{ // your class details }Blog | Twitter : @Hattan