Devsined
Devsined

Reputation: 3521

Entity Framework do not update Amazon RDS SQL database

I have a project with a DomainClasses C# library, a DataAccess C# library that used the Domian classes and finally a WebAPI that use the DataLayer. Also, I have an Amazon SQL RDS database.

With my context created in on of my controllers the database is not creating the tables related to my domain classes and if I use Console Manager and I Enable-Migrations on the Web API it showed me that the "No context type was found in the assembly 'WebApi'"

I have on the web config of my API the connection to AWS RDS db and I also named it the same name of my context in the datalayer. I am new on EF.

Every time that I run my application I was expecting that the databse has the tables but nothing happens there.

Thank you

This part of my code

Domain Classes:

public class SensorSession
    {
        public int Id { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public virtual UserTest User { get; set; }
        public virtual List<SensorTest> SensorsCollected { get; set; } 
    }
  public class SensorTest
    {
        public int Id { get; set; }
        public int HeartRate { get; set; }
        public int SkinConductivity { get; set; }
        public int Temperature {get; set;}
        public int Movement { get; set; }
        public int IndoorPositioning { get; set; }
        public DateTime CollectedDateTime { get; set; }
    }
   public class UserTest
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ProviderId { get; set; }
    }

DATA LAYER

namespace DataLayer
{
    public class TestDataContext:DbContext
    {
        public DbSet<SensorSession> SensorSessions { get; set; }
        public DbSet<SensorTest> SensorTests { get; set; }
        public DbSet<UserTest> UserTests { get; set; }
    }
}


public class TestDataInitialializer : DropCreateDatabaseAlways<TestDataContext>
    {
        protected override void Seed(TestDataContext context)
        {

            var testUser = new UserTest
            {
                Name = "Denis",
                ProviderId = 1
            };
            var sensorTest = new SensorTest
            {
                CollectedDateTime = DateTime.Now,
                HeartRate = 74,
                IndoorPositioning = 1,
                Movement = 2,
                SkinConductivity = 3,
                Temperature = 30
            };

            new List<SensorSession>
            {   new SensorSession
                {
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now,
                    User = testUser,
                    SensorsCollected = new List<SensorTest> { sensorTest }
                }
            }.ForEach(b => context.SensorSessions.Add(b));

            base.Seed(context);
        }
    }

WEB API

<connectionStrings>
    <add name="TestDataContext" providerName="System.Data.SqlClient" connectionString="my aws rds link"/>
     </connectionStrings>

Global as

protected void Application_Start()
        {
            Database.SetInitializer(new TestDataInitialializer());
...
}

Upvotes: 2

Views: 1774

Answers (1)

Julie Lerman
Julie Lerman

Reputation: 4622

The initialization won't happen until you do something to interact with the database from the context.. A query, a savechanges or even one of the direct executesql commands. There is also a command Database.Initialize that you can use to force it by passing in the force parameter of true.

Upvotes: 3

Related Questions