none
EF Migration Leaves Off a Field RRS feed

  • Question

  • I did a Code Based Migration with the following context I wanted to add to my database:

    public class SignalRUserContext : DbContext
       {
           public SignalRUserContext()
               : base("DefaultConnection")
           {
           }
     
           public DbSet<SignalRUser> Users { get; set; }
           public DbSet<SignalRConnection> Connections { get; set; }
       }
     
       public class SignalRUser
       {
           [Key]
           public string UserName { get; set; }
           public ICollection<SignalRConnection> Connections { get; set; }
       }
     
       public class SignalRConnection
       {
           [Key]
           public string ConnectionID { get; set; }
           public string UserAgent { get; set; }
           public bool Connected { get; set; }
       }

     then did:

    enable-migration AlphaFrontEndSiteASP.Models.SignalRUserContext -MigrationsDirectory:SignalRMigrations -Force

    Update-Database -configuration AlphaFrontEndSiteASP.SignalRMigrations.Configuration -Verbose

    where:

    public Configuration()
           {
               AutomaticMigrationsEnabled = true;
               ContextKey = "AlphaFrontEndSiteASP.Models.SignalRUserContext";
               MigrationsDirectory = @"SignalRMigrations";
           }

    It created the tables SignalRUser and SignalRConnection but on SignalRUser it did not contin the Connections column. It left it out. If I leave out the [Key] on Connections it gives me an error that that there is no key defined for the Connections table. I don't need a key on this table. How do I fix this?


    Tuesday, March 11, 2014 3:10 PM

Answers

  • Include is an extension method, so you have to have

    using System.Data.Entity;

    in the code file that contains the query.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:53 PM

All replies

  • >on SignalRUser it did not contin the Connections column

    In the relational model collections are modeled using a separate table and a foreign key.  So instead of SignalRUser having a "Connections" column, each SignalRConnection row has a SignalRUser_UserName column that points to the related SignalRUser row.  eg:

    CREATE TABLE [dbo].[SignalRConnections] (
        [ConnectionID] [nvarchar](128) NOT NULL,
        [UserAgent] [nvarchar](max),
        [Connected] [bit] NOT NULL,
        [SignalRUser_UserName] [nvarchar](128),
        CONSTRAINT [PK_dbo.SignalRConnections] PRIMARY KEY ([ConnectionID])
    )
    
    ALTER TABLE [dbo].[SignalRConnections] ADD CONSTRAINT [FK_dbo.SignalRConnections_dbo.SignalRUsers_SignalRUser_UserName] FOREIGN KEY ([SignalRUser_UserName]) REFERENCES [dbo].[SignalRUsers] ([UserName])

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 3:38 PM
  • I don't understand. That entire class/context is what I need to do SignalR Connection Management. I need to have each use have a number of connections. Do you want me to modify the tables as you have shown? Where is the SignalRUser table?


    • Edited by SamGwwwswes Tuesday, March 11, 2014 3:58 PM
    Tuesday, March 11, 2014 3:56 PM
  • I just pasted your SignalRUserContext into a project and ran it.  EF creates the tables that map to your model.  I thought you were just confused about how EF maps your object model to a relational model.

    The model you posted should work. 

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:03 PM
  • No, I just want to get it working. How did you get it to run? What commands? I showed my commands at the top but when I reference the following in code, it says Connections doesn't exist?

    using (var db = new SignalRUserContext())
    {
        //var user = db.Users
        //    .Include(u => u.Connections)
        //    .SingleOrDefault(u => u.UserName == name);
    }

    • Edited by SamGwwwswes Tuesday, March 11, 2014 4:10 PM
    Tuesday, March 11, 2014 4:06 PM
  • 1) File>New Project

    2) Right-Click "References" choose "Manage NuGet Packages", add EntityFramework

    3) Paste the SignalRUserContext code into a namespace in a code file

    4) Add "usings" to add the referenced namespaces

    using System.ComponentModel.DataAnnotations; using System.Data.Entity;

    5) Start using the DbContext

    Here's a walkthorough

    Data Developer Center > Learn > Entity Framework > Get Started > Code First to a New Database

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:14 PM
  • Its an existing database. That's the big reference. That's why I tried to do Migrations. It seems to have generated the right DB code. Why can't I reference Connections?
    • Edited by SamGwwwswes Tuesday, March 11, 2014 4:22 PM
    Tuesday, March 11, 2014 4:19 PM
  • I looked at both of these and they don't seem to help. I have an existing DB that I generated an EF Model from. I don't understand from the walkthroughs what to do.
    Tuesday, March 11, 2014 4:28 PM
  • So you generated an EF model from your database.  Then you can use the model to read and write to the database, as shown in those walkthroughs.  eg

    class Program 
    { 
        static void Main(string[] args) 
        { 
            using (var db = new BloggingContext()) 
            { 
                // Create and save a new Blog 
                Console.Write("Enter a name for a new Blog: "); 
                var name = Console.ReadLine(); 
     
                var blog = new Blog { Name = name }; 
                db.Blogs.Add(blog); 
                db.SaveChanges(); 
     
                // Display all Blogs from the database 
                var query = from b in db.Blogs 
                            orderby b.Name 
                            select b; 
     
                Console.WriteLine("All blogs in the database:"); 
                foreach (var item in query) 
                { 
                    Console.WriteLine(item.Name); 
                } 
     
                Console.WriteLine("Press any key to exit..."); 
                Console.ReadKey(); 
            } 
        } 
    }

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:31 PM
  • I think I got it by reverse engineering the db into the existing model. It generated this class:

    public partial class SignalRUser
        {
            public SignalRUser()
            {
                this.SignalRConnections = new HashSet<SignalRConnection>();
            }
        
            public string UserName { get; set; }
        
            public virtual ICollection<SignalRConnection> SignalRConnections { get; set; }
        }

    Why then can't i do:

    using (savitasEntities2 entities = new savitasEntities2())
                {
                    var user = entities.SignalRUsers
                        .Include(u => u.SignalRConnections)
                        .SingleOrDefault(u => u.UserName == name);
                }

    • Edited by SamGwwwswes Tuesday, March 11, 2014 4:38 PM
    Tuesday, March 11, 2014 4:33 PM
  • That looks like it should work.  Does it throw an exception?

    David


    David http://blogs.msdn.com/b/dbrowne/


    Tuesday, March 11, 2014 4:42 PM
  • Its in Red and Error 32 Cannot convert lambda expression to type 'string' because it is not a delegate type c:\worl\Project Alpha\Code\AlphaFrontEndSiteASP\AlphaFrontEndSiteASP\Hubs\AlphaHub.cs 232 30 AlphaFrontEndSiteASP
    Tuesday, March 11, 2014 4:43 PM
  • What version of EF are you using?  On older versions Include had to use a string, not a lambda.

    eg

                    var user = entities.SignalRUsers
                        
    .Include("SignalRConnections")
                        
    .SingleOrDefault(=> u.UserName == name);

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:48 PM
  • This is supposed to be 6.x
    Tuesday, March 11, 2014 4:49 PM
  • Include is an extension method, so you have to have

    using System.Data.Entity;

    in the code file that contains the query.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Tuesday, March 11, 2014 4:53 PM