Print Page | Close Window

Code First Database Generation Error

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3883
Printed Date: 25-Oct-2025 at 1:02pm


Topic: Code First Database Generation Error
Posted By: gregweb
Subject: Code First Database Generation Error
Date Posted: 10-Jan-2013 at 7:56pm
I have a very simple test project based on the CodeFirstWalk in Silverlight with a connection string in web.config to Sql Server.

When I fire it up and run it, when there is no existing database, it throws the following error:

System.Data.SqlClient.SqlException: CREATE DATABASE statement not allowed within multi-statement transaction.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

The odd part of this is that the database is in fact created, and the data properly saved to the database.

After the first run, there is no error as of course the database is already there and additional data is saved.

The code is very straightforward:

public class ProductEntities : EntityManager {
        public EntityQuery<Category> Categories { get; set; }
    }

    [ProvideEntityAspect]
    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
    }


public class MainPageViewModel
    {

        private ProductEntities Manager { get; set; }

        public MainPageViewModel() {
            SetManager();
            AddTestData();
        }

        private void AddTestData()
        {
            var cat = new Category { CategoryName = "Sweet Things on " + DateTime.Now.ToString("o") };
            Manager.AddEntity(cat);
            Manager.SaveChangesAsync();
        }

        private void SetManager()
        {
            Manager = new ProductEntities();
            Manager.EntityServerError += (s, e) =>
            {
               e.Handled = true; // we're dealing with it here
               MessageBox.Show(e.Exception.Message);
               Manager.RejectChanges(); // undo pending changes
            };
            Manager.SaveChangesAsync();
        }


    }

This is not a big issue, it just makes no sense to me.

Greg



Replies:
Posted By: kimj
Date Posted: 11-Jan-2013 at 9:52am
We had this problem back in the early days of DevForce Code First support and it was fixed in version 6.1.3, so it's not good that it's popped up again.
 
Which version/edition of SQL Server are you running? 


Posted By: gregweb
Date Posted: 11-Jan-2013 at 10:49am
Sql Server 2008 R2 and DevForce 7.0.3

But I think I know the issue - what was kicking off the database creation was adding an Entity and then saving it.

When I changed the kickoff to a query, then it ran without error. I think somehow the Database Creation and the save operation were somehow getting wrapped up into one transaction.

But this is resolved as far as I am concerned.

Greg


Posted By: kimj
Date Posted: 11-Jan-2013 at 12:02pm
OK, thanks for reporting this.



Print Page | Close Window