New Posts New Posts RSS Feed: Code First Database Generation Error
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Code First Database Generation Error

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: Code First Database Generation Error
    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
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post 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? 
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jan-2013 at 12:02pm
OK, thanks for reporting this.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down