locked
EF 4.1 not writing or creating the database RRS feed

  • Question

  • Hi,

       I am using EF 4.1 code first.  I had a list of POCO classes created and initially it created the associated database for me no problem.  However, I was updating my POCO classes  - Class A had a self-referential foreign key (id and parentID) and Class B had two properties defined as foreign keys - one attached to Class A.id and another to Class A.parentID.  And that generated a lot of errors about using the NO DELETE/UPDATE NO ACTION etc. 

       SO I removed all the foreign keys, but now EF won't even update my database.  I have tried dropping the database completely and getting EF to recreate it but that didn't happen.  I then created an empty database and ran my solution but EF still did not add the tables to it.  I then manually created all the tables and ran my solution with an insert against a single table. I can see my context is correctly pointed to my database and my object is holding correct values.  But EF is still not inserting into my database. 

       Can somebody please help? I've been staring at this thing for the last 3 days without any solution. Any help appreciated. 

     

    Update:  I've tried to debug the call to myContext.SaveChanges() and it seems to be throwing this exception

    -  ServerVersion '(((System.Data.Entity.DbContext)(context)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
     

    For some reason the connections seems to have been closed. Can anybody please tell me why that would happen ?

    Thank you,

     


    Wednesday, June 22, 2011 12:56 PM

Answers

  • Hi,
    I am not sure why Code First did not create tables. Could you please paste your code.
    As far as getting the original exception, I think it can be resolved by switching cascade deletes off on at least one of the associations.
    For example
    public class Child
    {
      public int Id { get; set; }
    
      public int ParentId1 { get; set; }
    
      [ForeignKey("ParentId1")]
      public Parent Parent1 { get; set; }
    
      public int ParentId2 { get; set; }
    
      [ForeignKey("ParentId2")]
      public Parent Parent2 { get; set; }
    }
    
    
    
    public class Parent
    {
    
      public int Id { get; set; }
    
      public string Description { get; set; }
    
    }
    
    public class MyContext : DbContext
    {
      public DbSet<Child> Children { get; set; }
      public DbSet<Parent> Parents { get; set; }
    
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
        modelBuilder.Entity<Child>()
          .HasRequired(t => t.Parent1)
          .WithMany()
          .HasForeignKey(t => t.ParentId1);
    
        modelBuilder.Entity<Child>()
            .HasRequired(t => t.Parent2)
            .WithMany()
            .HasForeignKey(t => t.ParentId2).WillCascadeOnDelete(false);
    
      }
    }
    


     For more information about this issue, see http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx
    thank you,
    Julia

    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Alan_chen Tuesday, July 5, 2011 9:29 AM
    Thursday, June 23, 2011 7:55 PM

All replies

  • Hi Cpaters,

    Welcome!

    According to your descritpion, I generated database by your POCO class and then changed the POCO class, you want to update your database, right?

    I couldn't just update the relationship between table, but we can set Database.SetInitial() to drop the database and recreate one. you can refer here:

    http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx

    Database.SetInitializer<ProductContext>(  new DropCreateDatabaseIfModelChanges<ProductContext>());
    
    

    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.

    Thursday, June 23, 2011 6:19 AM
  • Hi Alan,

       Thank you for your reply.  

        These were my steps

    - Create POCO classes

    - Run solution and get database generated - No problem

    - Update POCO classes with self-referential keys and foreign keys, add the Database. set initializer correctly and run solution again - ERROR stating that the foreign keys specified may cause cycles and to use NO ACTION for delete/update.

    - After that I remove the keys mentioned and try to rerun my solution.  Inserts do not work at this point.

    - I drop the database and run my solution, expecting EF to recreate the database.  But nothing happens

    - I then manually recreate only teh database but no tables and run my solution. Nothing again

    - I manually add all the tables. Delete the line in web.config and re-insert it and run the solution.  Now it seems to be looking at my database but I get the error -  ServerVersion '(((System.Data.Entity.DbContext)(context)).Database.Connection).ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException.  I checked my context item and it is correctly pointing to my <database name> on .\\SQLEXPRESS

    I have really no idea what to do now. I am going to try dropping the database again and removing the connection string from my web.config file to see if it will create a database using the namespace of my DbContext.  Else I am just going to have to re-think EF :-( 

    Any ideas, please do help.  I've got over this over and over and not gotten anywhere yet.  I could put in portions of my code if that will help.

    Thank you,

      

    Thursday, June 23, 2011 9:10 AM
  • Hi,
    I am not sure why Code First did not create tables. Could you please paste your code.
    As far as getting the original exception, I think it can be resolved by switching cascade deletes off on at least one of the associations.
    For example
    public class Child
    {
      public int Id { get; set; }
    
      public int ParentId1 { get; set; }
    
      [ForeignKey("ParentId1")]
      public Parent Parent1 { get; set; }
    
      public int ParentId2 { get; set; }
    
      [ForeignKey("ParentId2")]
      public Parent Parent2 { get; set; }
    }
    
    
    
    public class Parent
    {
    
      public int Id { get; set; }
    
      public string Description { get; set; }
    
    }
    
    public class MyContext : DbContext
    {
      public DbSet<Child> Children { get; set; }
      public DbSet<Parent> Parents { get; set; }
    
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
        modelBuilder.Entity<Child>()
          .HasRequired(t => t.Parent1)
          .WithMany()
          .HasForeignKey(t => t.ParentId1);
    
        modelBuilder.Entity<Child>()
            .HasRequired(t => t.Parent2)
            .WithMany()
            .HasForeignKey(t => t.ParentId2).WillCascadeOnDelete(false);
    
      }
    }
    


     For more information about this issue, see http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx
    thank you,
    Julia

    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Alan_chen Tuesday, July 5, 2011 9:29 AM
    Thursday, June 23, 2011 7:55 PM
  • Hi CPaters,

    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.

    Thursday, June 30, 2011 6:50 AM