Answered by:
EF 4.1 Fluent API - Add entity to database where table doesn’t exist DB

Question
-
Hi this is my first attempt with EF 4.0 - Fluent API. In database I dont have any table.
First I create simple POCO:
public class Book { public string Isbn { get; set; } public string Title { get; set; } public string Author { get; set; } }
Then I created context object:public class BookContext : DbContext { public DbSet<Book> Books { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Book>() .ToTable("BOOKS"); modelBuilder.Entity<Book>() .HasKey(s => s.Isbn); modelBuilder.Entity<Book>() .Property(s => s.Title) .HasColumnName("TITLE"); modelBuilder.Entity<Book>() .Property(s => s.Author) .HasColumnName("AUTHOR"); } }
On the end I tried add one entity to DB:private static void Add(Book book) { using(var ctx= new BookContext()) { ctx.Books.Add(book); ctx.SaveChanges(); } }
When I am calling Add method a get this exception:The type 'Study.EF.CodeFirst.Program+Book' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
My question is this error is cause because I don’t have Book table in DB or problem is in connection string?
<connectionStrings> <add name="BookContext" providerName="System.Data.SqlClient" connectionString="Server=jan-msi\sqlexpress;Database=EF;Integrated Security=True;MultipleActiveResultSets=True"/> </connectionStrings>
Tuesday, October 25, 2011 7:57 AM
Answers
-
Hi jminarik,
I was able to get your code working, just by commenting out the connection string. That way EF will create the database for you. It's best to let entity framework create the database for you even though there are ways to make it work by using an existing database, but I've only made it work one time and can't even remember how I got it to work! You can see some links on how to do that if you really want to use an existing database:
http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing-database/
Otherwise just let it create the database in your sqlexpress instance for you.
Tom OvertonTuesday, October 25, 2011 10:17 AM
All replies
-
Hi jminarik,
I was able to get your code working, just by commenting out the connection string. That way EF will create the database for you. It's best to let entity framework create the database for you even though there are ways to make it work by using an existing database, but I've only made it work one time and can't even remember how I got it to work! You can see some links on how to do that if you really want to use an existing database:
http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing-database/
Otherwise just let it create the database in your sqlexpress instance for you.
Tom OvertonTuesday, October 25, 2011 10:17 AM -
Hi,
I am writing to check the status of the issue on your side.Would you mind letting us know the result of the suggestions?
If you need further assistance, please feel free to let me know.I will be more than happy to be of assistance.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, October 31, 2011 2:13 AM