6

I'm trying to use the Entity Framework 4.1 RC with a SQL Server 2005 instance. I've created an empty database and I'd like to persist my POCO objects to it. My POCO looks like:

public class Cart
{
    public Cart()
    {
        this.CartId = Guid.NewGuid();

    }

    public Guid CartId { get; set; }
    public decimal TotalCost { get; set; }
    public decimal SubTotalCost { get; set; }
    public decimal Tax { get; set; }
    public decimal EstimatedShippingCost { get; set; }
}

My CartContext is:

public class CartContext : DbContext
{
    public DbSet<Cart> Carts { get; set; }
    public DbSet<Attribute> Attributes { get; set; }
    public DbSet<AttributeItem> AttributeItems { get; set; }
}

I have a connection string:

<add name="CartContext" connectionString="Server=myserver.mynetwork.net;User ID=MyUser;Pwd=mypassword;Initial Catalog=ExistingDb" providerName="System.Data.SqlClient" \>

When I try and add an object to the context and save it I get:

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.Carts'.

If I profile the database I can see the user connect, look for the database in sys.tables run this query:

SELECT TOP (1) 
[Extent1].[Id] AS [Id], 
[Extent1].[ModelHash] AS [ModelHash]
FROM [dbo].[EdmMetadata] AS [Extent1]
ORDER BY [Extent1].[Id] DESC 

Then attempt to insert my cart object. It never tries to create the Carts table. I'm guessing there's something wrong with the connection string, but I can't find examples anywhere on how to do this.

1
  • In closing your connection string you've got a backslash but it should be a forward slash ie /> not \> Commented Oct 22, 2011 at 9:40

1 Answer 1

8

DbContext will not create table just because it doesn't exists. Once you are using existing database you must also manually create tables or create custom initializer. Default initializers are only able to drop the database and create new one with all required tables.

You can for example call:

context.Database.Delete();
context.Database.Create();

Or:

context.Database.CreateIfNotExists();

Or:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
// You don't need to call this. Initialization takes place anyway if context 
// needs it but you can enforce initialization for example in the application 
// startup instead of before the first database operation
context.Database.Initialize(true); 
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, thanks. Unfortunately this isn't going to work for us at this time because we have an existing scheme that I'd like to add to. DropCreateDatabaseIfModelChanges<> is going to drop that existing scheme which is bad news! What a pity they didn't implement this at the schema level rather than for the entire database. I love using code first, it's so much nicer. The answer would be to write my own initializer I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.