none
EF 5.0 Code First in combination with SQL Azure RRS feed

  • Question

  • I'am unable to create tables in my pre-configured SQL Azure database. I want to use the code first approach of EF in a WPF project.

    I will give a small overview in code of my simple example.

    app.config file contains:

    As you can see, i'am trying to connect to an SQL azure database.

    <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="MuseContext" providerName="System.Data.SqlClient" connectionString="Server=tcp:myservername,1433;Database=dbname;User ID=myid;Password=mypassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;PersistSecurityInfo=True " /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> </configuration>

     public class MuseContext:DbContext
        {
            public MuseContext()
            {
                Database.SetInitializer<MuseContext>(new DropCreateDatabaseIfModelChanges<MuseContext>());
            }
            
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }
    
    
            public DbSet<Device> Devices { get; set; }
            public DbSet<Signal> Signals { get; set; }
        }
    public class Device
        {
            public int DeviceId { get; set; }
            public string Name { get; set; }
        }
     public Window1()
            {
                InitializeComponent();            
                
                _context = new MuseContext();
                _context.Devices.Add(new Device{ DeviceId = 1, Name = "sdqfsdf" });
                _context.SaveChanges();
    }

    The probmen now is that i did't add a new device to my azure db, but it's added to my local db.

    When i restart app, i can see the previously added element.

    How can i ensure that my app is really pointing to azure db instead of local db?

     
    Wednesday, December 5, 2012 3:44 PM

Answers

  • Hi JVGVAG,

    Welcome to the MSDN forum.

    Please try to use this:

        public class MuseContext:DbContext
        {
            public MuseContext()
            {
                Database.SetInitializer<MuseContext>(new DropCreateDatabaseIfModelChanges<MuseContext>());
            }
             
            public MuseContext(String connectionString) : base(connectionString)
            {
                Database.SetInitializer<MuseContext>(new DropCreateDatabaseIfModelChanges<MuseContext>());
            }
            
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }
    
    
            public DbSet<Device> Devices { get; set; }
            public DbSet<Signal> Signals { get; set; }
        }
            public Window1()
            {
                InitializeComponent();            
                
                _context = new MuseContext("MuseContext");//Or use _context = new MuseContext("name=MuseContext");
                _context.Devices.Add(new Device{ DeviceId = 1, Name = "sdqfsdf" });
                _context.SaveChanges();
    }

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Thursday, December 20, 2012 7:41 AM
    Friday, December 7, 2012 5:50 AM

All replies

  • Can nobody help me in solving this probmen?

    According information from diffrent forums, it should be related to format of connection string.

    How do i need to build up my connection string when using EF and SQL Azure?

    Thursday, December 6, 2012 10:10 AM
  • Hi JVGVAG,

    Welcome to the MSDN forum.

    Please try to use this:

        public class MuseContext:DbContext
        {
            public MuseContext()
            {
                Database.SetInitializer<MuseContext>(new DropCreateDatabaseIfModelChanges<MuseContext>());
            }
             
            public MuseContext(String connectionString) : base(connectionString)
            {
                Database.SetInitializer<MuseContext>(new DropCreateDatabaseIfModelChanges<MuseContext>());
            }
            
            protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }
    
    
            public DbSet<Device> Devices { get; set; }
            public DbSet<Signal> Signals { get; set; }
        }
            public Window1()
            {
                InitializeComponent();            
                
                _context = new MuseContext("MuseContext");//Or use _context = new MuseContext("name=MuseContext");
                _context.Devices.Add(new Device{ DeviceId = 1, Name = "sdqfsdf" });
                _context.SaveChanges();
    }

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Thursday, December 20, 2012 7:41 AM
    Friday, December 7, 2012 5:50 AM
  • Hi Alexander,

    Thanks for your reply.

    I think i made a step forward in accessing the SQL Azure DB.

    Now i'am getting other error, "Windows logins are not supported in this version of SQL Server."

    Do i need to change some settings  in SQL Azure DB?

    Sunday, December 9, 2012 12:26 PM
  • Hi JVGVAG,

    Frankly speaking, I am not very familiar with SQL Azure, so I recommend you try SQL Azure forum for better support: http://social.msdn.microsoft.com/Forums/en-US/category/windowsazureplatform

    In addition, I found this page you could check whether it helps: http://stackoverflow.com/questions/9818354/how-to-resolve-azure-windows-logins-are-not-supported-in-this-version-of-sql-se

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 11, 2012 9:10 AM
  • Thanks for you help, Alexander!!
    Thursday, December 27, 2012 7:55 AM